Learn Jenkins continuous integration (three) Jenkins project build trigger, parameterized build and email sending

One, build trigger

1. Commonly used build triggers

Jenkins has 4 built-in build triggers:

  1. Trigger remote construction
    . The authentication token is a token set by ourselves and is generally encrypted.

Insert picture description here
Insert picture description here

Then use the browser address bar to visit JENKINS_URL/job/wxpublic/build?token=TOKEN_NAME 或者 /buildWithParameters?token=TOKEN_NAME
JENKINS_URL to replace the address of Jenkins, and replace TOKEN_NAME with the set token: it http://139.196.231.13:8888/job/wxpublic/build?token=liushihaocan be built immediately through remote access.

  1. Triggered after other projects are build (Build after other projects are build)
    That is, when the project of interest is built, the project will also trigger the build
    Insert picture description here

  2. Build periodically
    Insert picture description here

The timing strings from left to right are: time-sharing day month week

Some examples of timing expressions:
Build every 30 minutes: H represents the formal parameter H/30 * * * * 10:02 10:32
Build every 2 hours: HH/2 * * *
8 o'clock, 12 o'clock every day , 22:00, build 3 times a day: (multiple time points are separated by commas) 0 8,12,22 * * * H 12 will be constructed at 12 noon every day * * *
H 18 will be constructed at 18:00 every day * * * Every 10 minutes within the first half hour of each hour H(0-29)/10 * * * *Once
every two hours, every weekday from 9 am to 5 pm (maybe 10 am: 38, 12:38 pm, 2:38 pm, 4:38 pm) HH(9-16)/2 * * 1-5

  1. Poll SCM (Poll SCM)
    Insert picture description here

Polling SCM is to scan the code in the local code warehouse for changes when specified, and trigger project construction if the code changes.
Note: This time the trigger is built, Jenkins will periodically scan the code of the entire local project, increasing the overhead of the system, and it is not recommended.
Note: The two minutes set will not necessarily be built, only after the project code is changed.

2. Git hook automatically triggers the build (*)

Just now we saw that in the built-in build trigger of Jenkins, polling SCM can realize Gitlab code update and automatic project construction, but the performance of this solution is not good. Is there a better solution? Yes. It uses Gitlab's webhook to push the code to the warehouse, which immediately triggers the automatic construction of the project.
To install the Gitlab Hook plug-in, you need to install two plug-ins: Gitlab Hook and GitLab.
But because I am using Gogs to build a personal warehouse, I need to install the gogs plug-in
Insert picture description here
Jenkins to set up automatic construction
Insert picture description here
Insert picture description here

3. Jenkins parameterized construction

Insert picture description here

4. Configure the mailbox server to send the build result

Insert picture description here

Insert picture description here
Find Open (qq mailbox) in the mailbox settings
Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here

Insert picture description here
email template

<!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>
            <b><font color="#0B610B">变更信息:</font></b>
            <hr size="2" width="100%" align="center" />
        </td>
    </tr>
    <tr>
        <td>
            <ul>
                <li>上次构建成功后变化 :  ${
    
    CHANGES_SINCE_LAST_SUCCESS}</a></li>
            </ul>
        </td>
    </tr>
    <tr>
        <td>
            <ul>
                <li>上次构建不稳定后变化 :  ${
    
    CHANGES_SINCE_LAST_UNSTABLE}</a></li>
            </ul>
        </td>
    </tr>
    <tr>
        <td>
            <ul>
                <li>历史变更记录 : <a href="${PROJECT_URL}changes">${
    
    PROJECT_URL}changes</a></li>
            </ul>
        </td>
    </tr>
<!--    <tr>-->
<!--        <td>-->
<!--            <ul>-->
<!--                <li>变更集:${
    
    JELLY_SCRIPT,template="html"}</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><textarea cols="80" rows="30" readonly="readonly" style="font-family: Courier New">${
    
    BUILD_LOG,maxLines=100}</textarea>
        </td>
    </tr>
</table>
</body>

</html>

Add sending mail script:
Insert picture description here

Insert picture description here
Send mail script:

pipeline {
    
    
    agent any

    stages {
    
    
        stage('拉取代码') {
    
    
            steps {
    
    
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '00b15bbd-d044-4db1-9eb6-063c16781dc4', url: 'http://139.196.231.13:3000/liushihao/monitor.git']]])
            }
        }
        stage('编译构建') {
    
    
            steps {
    
    
                sh label: '', script: 'mvn clean package'
            }
        }
         stage('构建完成') {
    
    
            steps {
    
    
                echo '构建完成'
            }
        }
    }
    post {
    
    
          always {
    
    
             emailext(
                subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',
                body: '${FILE,path="email.html"}',
                to: '******@qq.com,******[email protected],******[email protected]'
             )
          }
    }
}

effect:
Insert picture description here

Guess you like

Origin blog.csdn.net/DreamsArchitects/article/details/109320993