您好,登錄后才能下訂單哦!
小編給大家分享一下如何使用Jenkins Pipeline自動(dòng)化構(gòu)建發(fā)布Java項(xiàng)目的方法,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
簡(jiǎn)介
Pipeline,簡(jiǎn)而言之,就是一套運(yùn)行于Jenkins上的工作流框架,將原本獨(dú)立運(yùn)行于單個(gè)或者多個(gè)節(jié)點(diǎn)的任務(wù)連接起來(lái),實(shí)現(xiàn)單個(gè)任務(wù)難以完成的復(fù)雜流程編排與可視化。
Pipeline是Jenkins2.X的最核心的特性,幫助Jenkins實(shí)現(xiàn)從CI到CD與DevOps的轉(zhuǎn)變。
一,創(chuàng)建pipeline項(xiàng)目
二,清除部分歷史構(gòu)建
三,參數(shù)化構(gòu)建
這里使用三個(gè)參數(shù),分別對(duì)應(yīng)是否拉取代碼,項(xiàng)目名稱,以及發(fā)版選項(xiàng)
四,編寫pipeline腳本
選擇pipeline script
編寫pipeline腳本
def userMap def proMap = ['api': 'test_api', 'report':'test_report', 'job': 'test_job'] //定義項(xiàng)目字典 def deployMap = ['test-pipeline': 'test-pro.sh'] //定義發(fā)版腳本 def predeployMap = ['test-pipeline': 'test-pre.sh'] //定義預(yù)發(fā)布腳本 def rollbackMap = ['test-pipeline': 'test-pro-rollback.sh'] //定義回滾腳本 pipeline { agent any //parameters { string(defaultValue: '', name: 'PULL_FLAG', description: '請(qǐng)根據(jù)發(fā)布類型進(jìn)行選擇發(fā)布:\n1,輸入-TESTING-發(fā)布-最新代碼-到灰度\n2,輸入-LATEST-發(fā)布-最新代碼-到生產(chǎn)\n3,輸入-版本號(hào)-發(fā)布-制定版本-到生產(chǎn) ' ) } stages { stage('Checkout') { when { //判斷是否要拉取代碼 environment name: 'PULL_FLAG', value: 'yes' } steps { echo "${PULL_FLAG}" //拉取代碼 checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CleanBeforeCheckout']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '797fddfc-d9d6-42a7-9a2b-38421a6963b0', url: 'git@git.test.com:test/test.git']]]) echo 'Checkout' } } stage('Build') { when { environment name: 'PULL_FLAG', value: 'yes' } steps { echo 'Building' // 使用maven進(jìn)行構(gòu)建 sh 'mvn clean install -Dmaven.test.skip=true -Pprod' } } stage('Push package') { when { environment name: 'PULL_FLAG', value: 'yes' } steps { echo 'push package' //上傳war包至跳板機(jī) sh 'sh /srv/shell/PAY_scp_pipe.sh ' } } stage('Deploy/Rollback') { steps { //定義發(fā)版密碼校驗(yàn),只有輸入口令正確,才會(huì)發(fā)版到線上機(jī)器,密碼保存在環(huán)境變量中 timeout(60) { script { userMap = input message: 'please input password', ok: 'ok', submitter: 'admin', parameters: [password(name:'password', defaultValue: '', description: '發(fā)布代碼口令' )], submitterParameter: 'admin' if (userMap['password'].toString() == PASSWORD) { echo '密碼正確' if (DEPLOY_FLAG == 'deploy') { echo "發(fā)版" sh "ssh test 'cd /data/faban && sh ${deployMap[env.JOB_NAME]} ${proMap[PROJECT]}'" }else if (DEPLOY_FLAG == 'predeploy') { echo '預(yù)發(fā)布' sh "ssh test 'cd /data/faban/pre-release && sh ${predeployMap[env.JOB_NAME]} ${proMap[PROJECT]}'" }else if (DEPLOY_FLAG == 'rollback') { echo '回滾' sh "ssh test 'cd /data/faban && sh ${rollbackMap[env.JOB_NAME]} ${proMap[PROJECT]}'" } }else { echo '密碼錯(cuò)誤' echo "${env.JOB_NAME}" } } } } } } post { always { echo 'This will always run' //發(fā)版結(jié)束后刪除jenkins workspace下的臨時(shí)目錄 deleteDir() } success { echo 'This task is successful!' //記錄日志信息 sh """ printf '%s %s %s %s %s %s' `date +'%F %H:%M:%S'` "${env.JOB_NAME}" "${proMap[PROJECT]}" "${DEPLOY_FLAG}" "success!\n" >> /srv/jk_logs/jk.log """ } } }
五,用到的shell腳本
1, 上傳war包的腳本 /srv/shell/TEST_scp_pipe.sh
#!/bin/bash scp -r /srv/jenkins/workspace/test/test/api/target/api.war test:/data/faban/server_war/risk_api.war scp -r /srv/jenkins/workspace/test/test/report/target/report.war test:/data/faban/server_war/risk_report.war scp -r /srv/jenkins/workspace/test/test/job/target/job.war test:/data/faban/server_war/risk_job.war
2, 發(fā)版腳本(跳板機(jī))
#!/bin/bash project="$1" java_config=/data/faban/java_config war_rebuild=/data/faban/war_rebuild if [ $# -lt 1 ]; then echo "usage: $0 <project>" exit 1 fi case "$project" in test_api) tomcat=/opt/tomcat_api server="test" ;; test_report) tomcat=/opt/tomcat_report server="test" ;; test_job) tomcat=/opt/tomcat_job server="test" ;; esac rm -rf $war_rebuild/* rsync -av /data/faban/server_war/$project.war /data/faban/war_rebuild cd $war_rebuild ssh $server "pkill -9 -f $tomcat" ssh $server "rm -rf $tomcat/webapps/ROOT; mv $tomcat/webapps/ROOT.war $tomcat/webapps/ROOT.war.bak" scp $project.war $server:$tomcat/webapps/ROOT.war ssh $server "chown www.www $tomcat/webapps/ROOT.war" ssh $server "su - www -c $tomcat/bin/startup.sh" sleep 5
3, 回滾腳本(跳板機(jī))
#!/bin/bash project="$1" if [ $# -lt 1 ]; then echo "usage: $0 <project>" exit 1 fi case "$project" in test_api) tomcat=/opt/tomcat_api server="test" ;; test_report) tomcat=/opt/tomcat_report server="test" ;; test_job) tomcat=/opt/tomcat_job server="test" ;; esac ssh $server "pkill -9 -f $tomcat" ssh $server "rm -rf $tomcat/webapps/ROOT; mv $tomcat/webapps/ROOT.war.bak $tomcat/webapps/ROOT.war" ssh $server "su - www -c $tomcat/bin/startup.sh" sleep 5
看完了這篇文章,相信你對(duì)“如何使用Jenkins Pipeline自動(dòng)化構(gòu)建發(fā)布Java項(xiàng)目的方法”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。