Jenkins 構建觸發器

1.觸發遠程構建 (例如,使用腳本)

打開url:http://192.168.197.102:8888/job/com-web-yunan/build?token=6666
就會觸發構建

2.其它工程構建後觸發

3.定時構建觸發器

表達式:分 時 日 月 周(H代表形參)

H/2 * * * * 代表每隔2分鐘觸發一次

4.輪循SCM

jenkins會定時掃描代碼倉庫的代碼是否有變更,有變更就會構建。但是會增大系統開銷,不建議使用。

5.git hook自動觸發構建

當gitlab上有push操作時構建

安裝GitLab和Gitlab Hook插件

gitlab配置

打開Admin area

Settings->Network

然後在項目下Settings->Integrations

粘貼jenkins產生的地址

點擊ADD

打開jenkins的Configure System

Gitlab Web Hook處點擊高級

 

6.參數化構建

在腳本里讀取branch參數 ${branch}

pipeline {
   agent any

   stages {
      stage('pull code') {
         steps {
            checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '5e5900e1-cb72-4e80-a1a1-18975b0f9d83', url: '[email protected]:root/com-web-yunan.git']]])
         }
      }
      stage('build project') {
         steps {
           sh label: '', script: 'mvn clean package'
         }
      }
      stage('publish project') {
         steps {
           deploy adapters: [tomcat9(credentialsId: 'c5963b76-15ef-41e8-8ebe-acdd81bc0acb', path: '', url: 'http://192.168.197.103:8080/')], contextPath: null, war: 'target/*.war'
         }
      }
   }
}

7.配置郵箱服務器發送構建結果

安裝Email Extension Template插件

郵箱服務器需要開啓POP3/SMTP/IMAP

打開jenkins的Configure System

 

 準備郵件發送的模板,在項目根目錄下新建email.html


<!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>項目名稱&nbsp;:&nbsp;${PROJECT_NAME}</li>
          <li>構建編號&nbsp;:&nbsp;第${BUILD_NUMBER}次構建</li>
          <li>觸發原因:&nbsp;${CAUSE}</li>
          <li>構建日誌:&nbsp;<a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
          <li>構建&nbsp;&nbsp;Url&nbsp;:&nbsp;<a href="${BUILD_URL}">${BUILD_URL}</a></li>
          <li>工作目錄&nbsp;:&nbsp;<a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>
          <li>項目&nbsp;&nbsp;Url&nbsp;:&nbsp;<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> 
<tr> 
     <td>
         <ul> 
              <li>歷史變更記錄 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li>
         </ul></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>Test Logs (if test has ran): <a href="${PROJECT_URL}ws/TestResult/archive_logs/Log-Build-${BUILD_NUMBER}.zip">${PROJECT_URL}/ws/TestResult/archive_logs/Log-Build-${BUILD_NUMBER}.zip</a> <br /> <br /> </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>

腳本追加:

   post {
      always {
		emailext body: '${FILE, path="email.html"}', subject: '構建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!', to: '[email protected]'
	  }
   }

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章