以前用的jenkins自由風格發布代碼。界面丑陋,出現問題位置不夠清晰。今天改進一下流程使用jenkins pipeline構建項目。
學習使我快樂
步驟一、安裝pipeline插件
點擊系統管理->插件管理->可選插件,輸入pipeline,選擇如下插件安裝
步驟二、Jenkins設置郵箱相關參數
Manage Jenkins->Configure System
步驟三、創建流水線項目
新建任務->選擇流水線
描述瞎寫,自己區分項目內容
丟棄舊的構建,選擇構建天數,輸入保持構建的最大個數。然后直接拉到最下面,在流水線里選擇pipeline script或者pipeline script from SCM
先貼一下自己常用的一段代碼
def createVersion() { // 定義一個版本號作為當次構建的版本,輸出結果 20191210175842_69 return new Date().format('yyyyMMddHHmmss') + "_${env.BUILD_ID}" } pipeline{ agent any parameters { gitParameter branch: '', branchFilter: '.*', defaultValue: 'master', description: '選擇代碼分支', name: 'branch', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH' choice choices: ['張曉琳', '裴明明', '劉新潮'], description: '填寫開發人員', name: 'developer' text defaultValue: '', description: '請填寫更新日志', name: 'record' } environment { def ip = "ip地址" def remotedir = "/home/wwwroot/hxtPayAdmin" def _version = createVersion() def tmpdir = "/tmp/hxtPayAdmin_${_version}" def today = sh(script: "echo `date +%Y_%m_%d`", returnStdout: true).trim() } stages{ stage('拉取代碼'){ steps{ checkout([$class: 'GitSCM', branches: [[name: '$branch']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '9df82ae3-5563-4d32-808b-00e8aa9ae26c', url: 'git@192.168.80.13:php/hxtPayAdmin.git']]]) } } stage('備份服務器文件'){ steps{ sh label: '', script: '''ansible ${ip} -m file -a "path=$tmpdir state=directory" ansible ${ip} -m shell -a "rsync -aqp --exclude=\'storage\' --exclude=\'*.csv\' $remotedir $tmpdir"''' } } stage('發布文件到服務器'){ steps { sh label: '', script: 'chown www.www . -R' sh label: '', script: 'ansible ${ip} -m synchronize -a "src=${WORKSPACE}/ dest=$remotedir delete=no archive=yes compress=yes rsync_opts=--exclude=\'.git\',--exclude=\'storage\',--exclude=\'vendor\',--exclude=\'.env\'"' } } } post('send mail'){ always{ emailext( subject: '構建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} -${BUILD_STATUS}!', body: '${FILE,path="/root/script/email.html"}', to: 'guoyabin@96199.com.cn') echo "-----構建狀態: ${currentBuild.currentResult}-----" sh label: '', script: ''' now=`date "+%Y_%m_%d_%H_%M_%S"` echo "時間: $now" >> /root/update_log/$today echo "開發者: $developer" >> /root/update_log/$today echo "git地址: git@192.168.80.13:php/hxtPayAdmin.git" >> /root/update_log/$today echo "項目名稱: ${JOB_NAME}" >> /root/update_log/$today echo "構建編號: $BUILD_NUMBER" >> /root/update_log/$today echo "分支名字: $branch" >> /root/update_log/$today echo "更新日志: $record" >> /root/update_log/$today ''' sh label: '', script: "echo 構建狀態: ${currentBuild.currentResult} >> /root/update_log/$today" sh label: '', script: "echo -e \'-----------------------------------\\n\' >> /root/update_log/$today" } } }
貼一下body里的發email代碼
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次構建日志</title> </head> <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0"> <table width="95%" cellpadding="0" cellspacing="0" style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans- serif"> <tr> <td>(本郵件是程序自動下發的,請勿回復!)</td> </tr> <tr> <td><h2> <font color="#0000FF">構建結果 - ${BUILD_STATUS}</font> </h2></td> </tr> <tr> <td><br /> <b><font color="#0B610B">構建信息</font></b> <hr size="2" width="100%" align="center" /></td> </tr> <tr> <td> <ul> <li>項目名稱 : ${PROJECT_NAME}</li> <li>構建編號 : 第${BUILD_NUMBER}次構建</li> <li>觸發原因: ${CAUSE}</li> <li>構建日志: <a href="${BUILD_URL}console">${BUILD_URL}console</a></li> <li>構建 Url : <a href="${BUILD_URL}">${BUILD_URL}</a></li> <li>工作目錄 : <a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li> <li>項目 Url : <a href="${PROJECT_URL}">${PROJECT_URL}</a></li> </ul> </td> </tr> <tr> <td><b><font color="#0B610B">Changes Since Last Successful Build:</font></b> <hr size="2" width="100%" align="center" /></td> </tr> 編寫Jenkinsfile添加構建后發送郵件 <tr> <td> <ul> <li>歷史變更記錄 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li> </ul> ${CHANGES_SINCE_LAST_SUCCESS,reverse=true, format="Changes for Build #%n:<br />%c<br />",showPaths=true,changesFormat="<pre>[%a]<br/>%m</pre>",pathFormat=" %p"} </td> </tr> <tr> <td><b>Failed Test Results</b> <hr size="2" width="100%" align="center" /></td> </tr> <tr> <td><pre style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">$FAILED_TESTS</pre> <br /></td> </tr> <tr> <td><b><font color="#0B610B">構建日志 (最后 100行):</font></b> <hr size="2" width="100%" align="center" /></td> </tr> <tr> <td><textarea cols="80" rows="30" readonly="readonly" style="font-family: Courier New">${BUILD_LOG,maxLines=100}</textarea> </td> </tr> </table> </body> </html>
步驟四、點擊構建,測試,查看日志流程。
五、其他
pipeline語法可以從流水線語法這里檢查學習,