jenkisn Pipeline的流水线发布,自动化部署
创建一个流水线job,这只是个简单的流水线发布教程,写的不好~
创建好后进入改job设置
你可以在参数化构建中加入这些需要提前设定的参数,以自己的Jenkinsfile需要做添加
然后我们看下Jenkins-a的内容
node {
def JAVA_HOME = tool 'jdk' #定义个局部变量,设置jdk环境
env.PATH = "${JAVA_HOME}/bin:${env.PATH}"
stage('拉取代码') {
#检出代码的命令可以用上面的生成器自己生成
checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '', locations: [[cancelProcessOnExternalsFail: true, credentialsId: '', depthOption: 'infinity', ignoreExternalsOption: true, local: '.', remote: 'svn.jsc.com']], quietOperation: true, workspaceUpdater: [$class: 'UpdateUpdater']])
}
stage('构建') {
if ("${Status}"== 'Deploy') {
sh "/usr/local/maven/bin/mvn clean install -pl $module -am -U -Dmaven.test.skip=true"
echo '需要构建'
} else {
echo '不需要构建'
}
}
def web_name = "${JOB_NAME}"
stage('Status判断') {
if ("${Status}"== 'Deploy') {
jar_file = sh (
script: 'find $WORKSPACE/$module/target -name *.war',
returnStdout: true
).trim()
sh 'mkdir -p $WORKSPACE/bak/$BUILD_ID'
sh "(cp -a '${jar_file}' $WORKSPACE/bak/$BUILD_ID)"
echo '部署'
} else {
jar_file = sh (
script: 'find $WORKSPACE/bak/$Rollback_tag -name *.war',
returnStdout: true
).trim()
echo '回滚'
}
}
war_file_name = sh (
script: "(basename '${jar_file}')",
returnStdout: true
).trim()
config_file_name = sh (
script: 'basename ${config_file}',
returnStdout: true
).trim()
old_BUILD_NUMBER = sh (
script: "(awk 'BEGIN{print '${BUILD_NUMBER}' - 1 }')",
returnStdout: true
).trim()
stage('打印部署参数') {
#这些参数都后面ansible发布需要用的,不一定适合你
echo "jar_file: ${jar_file}"
echo "war_file_name: ${war_file_name}"
echo "config_file: ${config_file}"
echo "config_file_name: ${config_file_name}"
echo "job_name: ${web_name}"
echo "old_BUILD_NUMBER: ${old_BUILD_NUMBER}"
}
stage('部署') {
#这里你可以用ansible部署到机器上,或者用其他什么的,下面只做个示范照抄无效
sh "/usr/bin/ansible-playbook /etc/ansible/playbook/deploy_test.yaml -f 5 -e jar_file=${jar_file} -e config_file_name=${config_file_name} -e host=${host} "
}
}
最后的效果