在jenkins官方文档是这样介绍pipeline的:Jenkins Pipeline (or simply "Pipeline") is a suite of plugins which supports implementing and integrating continuous delivery pipelinesinto Jenkins.它的意思就是pipeline是一套jenkins官方提供的插件,它可以用来在jenkins中实现和集成连续交付。
GIT构建:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
pipeline { agent any options { disableConcurrentBuilds() } parameters { gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'BRANCH', type: 'PT_BRANCH' } stages { stage('用户鉴权') { steps { echo '用户权限验证.....' wrap([$class: 'BuildUser']) { script { BUILD_USER_ID = "${env.BUILD_USER_ID}" BUILD_USER = "${env.BUILD_USER}" BUILD_USER_EMAIL = "${env.BUILD_USER_EMAIL}" } } script{ if ( "${BUILD_USER}" == "aaabyrd" ) { sh 'echo "非法授权用户....." && exit 1' } else { echo "用户权限合法....." } } } } stage('拉取代码') { steps { echo '拉取程序代码.....' echo '${PUBLISH-SERVER}' git branch: "${params.BRANCH}", credentialsId: 'xxxxxxx', url: 'http://note.t4x.org/site.git' } } stage('代码打包') { steps { echo '前端打包操作.....' sh '/usr/local/bin/cnpm install && /usr/local/bin/cnpm run build' } } } } |
前端构建:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
pipeline { agent any options { disableConcurrentBuilds() } parameters { choice choices: ['dev', 'test', 'others'], description: 'dev:x.x.x.x\ntest:x.x.x.x', name: 'project_deploy' } environment { dev_ip = "x.x.x.x" test_ip = "x.x.x.x" push_user = "xxx" http_user = "xxxx" remote_port = "xx" site_dir = "/xxx/xxx/xxx" program_dir = "xxxx" } stages { stage('用户鉴权') { steps { echo '用户权限验证.....' wrap([$class: 'BuildUser']) { script { BUILD_USER_ID = "${env.BUILD_USER_ID}" BUILD_USER = "${env.BUILD_USER}" BUILD_USER_EMAIL = "${env.BUILD_USER_EMAIL}" } } script{ if ( "${BUILD_USER}" == "aaabyrd" ) { sh 'echo "非法授权用户....." && exit 1' } else { echo "用户权限合法....." } } } } stage('授权更改') { steps { echo '授权更改.....' script{ switch("$project_deploy"){ case "dev": sh 'ssh -p{remote_port} ${push_user}@${dev_ip} "sudo chown -R ${push_user}.${push_user} ${site_dir}"' break case "test": sh 'ssh -p${remote_port} ${push_user}@${test_ip} "sudo chown -R ${push_user}.${push_user} ${site_dir}"' break default: echo "############ Error ############" break } } } } stage('代码推送') { steps { echo '前端代码推送.....' script{ switch("$project_deploy"){ case "dev": sh 'cd ${program_dir} && scp -rp * ${push_user}@${dev_ip}:${site_dir}' break case "test": sh 'cd ${program_dir} && scp -rp * ${push_user}@${test_ip}:${site_dir}' break default: echo "############ Error ############" break } } } } stage('版本推送') { steps { echo '版本号码推送.....' script{ switch("$project_deploy"){ case "dev": sh 'echo $(date +%Y%m%d)_${BUILD_NUMBER} > version.txt && scp -P${remote_port} version.txt ${push_user}@${dev_ip}:${site_dir}' break case "test": sh 'echo $(date +%Y%m%d)_${BUILD_NUMBER} > version.txt && scp -P${remote_port} version.txt ${push_user}@${test_ip}:${site_dir}' break default: echo "############ Error ############" break } } } } stage('授权恢复') { steps { echo '授权恢复.....' script{ switch("$project_deploy"){ case "dev": sh 'ssh -p${remote_port} ${push_user}@${dev_ip} "sudo chown -R ${http_user}.root ${site_dir}"' break case "test": sh 'ssh -p${remote_port} ${push_user}@${test_ip} "sudo chown -R ${http_user}.root ${site_dir}"' break default: echo "############ Error ############" break } } } } } } |
后端构建:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
pipeline { agent any options { disableConcurrentBuilds() } parameters { choice choices: ['dev', 'test', 'others'], description: 'dev:xxx\ntest:xxx', name: 'project_deploy' } environment { dev_ip = "xxx" test_ip = "xxx" pushuser="xxx" jar_name = "xxx" scripts_name = "xxx" site_dir = "xxx" program_dir = "xxx" } stages { stage('用户鉴权') { steps { echo '用户权限验证.....' wrap([$class: 'BuildUser']) { script { BUILD_USER_ID = "${env.BUILD_USER_ID}" BUILD_USER = "${env.BUILD_USER}" BUILD_USER_EMAIL = "${env.BUILD_USER_EMAIL}" } } script{ if ( "${BUILD_USER}" == "aaabyrd" ) { sh 'echo "非法授权用户......" && exit 1' } else { echo "用户权限合法......" } } } } stage('程序推送') { steps { echo '应用程序推送.......' script{ switch("$project_deploy"){ case "dev": sh 'cd ${program_dir} && scp -rp *.jar ${pushuser}@${dev_ip}:${site_dir}/${jar_name}' break case "test": script{ if ( "${BUILD_USER}" != "byrd" ) { sh 'echo "非法授权用户......" && exit 1' } else { echo "用户权限合法......" } } sh 'cd ${program_dir} && scp -rp *.jar ${pushuser}@${test_ip}:${site_dir}/${jar_name}' break default: echo "############ Error ############" break } } } } stage('版本推送') { steps { echo '版本序号推送.......' script{ switch("$project_deploy"){ case "dev": sh 'echo $(date +%Y%m%d)_${BUILD_NUMBER} > version.txt && scp -P22 version.txt ${pushuser}@${dev_ip}:${site_dir}' break case "test": sh 'echo $(date +%Y%m%d)_${BUILD_NUMBER} > version.txt && scp -P22 version.txt ${pushuser}@${test_ip}:${site_dir}' break default: echo "############ Error ############" break } } } } stage('程序重启') { steps { echo '程序重新启动.......' script{ switch("$project_deploy"){ case "dev": sh 'ssh ${pushuser}@${dev_ip} "cd /data/scripts/ && ./${scripts_name} dev"' break case "test": sh 'ssh ${pushuser}@${test_ip} "cd /data/scripts/ && ./${scripts_name} test"' break default: echo "############ Error ############" break } } } } } } |
申明:除非注明Byrd's Blog内容均为原创,未经许可禁止转载!详情请阅读版权申明!