Use Jenkinsfile to realize continuous integration of interface automation testing

1. Jenkins Pipeline

Pipeline: pipeline

1. What is the type of pipeline?

In simple terms, pipeline is a set of workflow framework running on Jenkins, which connects things that originally run independently on a single or multiple joints, and realizes complex process arrangement and visualization work that is difficult to complete with a single task.

insert image description here

2. Several steps in the pipeline:

General : Global information for configuration tasks, such as project description, creation of global parameters, other global configurations of the pipeline, etc. In fact, it is not much different from the General in the free style.
Build trigger : If we need to trigger the construction of tasks through other places: for example, scheduled construction, use scripts to remotely trigger the construction operation, and automatically trigger the construction of this task after other tasks are built, etc.
If we are just starting to use it, we don't need to configure any configuration in the build trigger, and it does not affect our task build. It just needs us to actively click the build button to build.
Advanced project options : configure the display name of the project, if not configured, it will be displayed by default.
Pipeline : This module is the highlight of our pipeline configuration. Implement various build dependency operations through pipeline scripts. (Related construction processes such as building Android projects, the Gradle plug-in is still required)
insert image description here
After a period of time, request the jenkins server to compare the code, and if the code is different, perform the build operation.

2. Use Jenkinsfile to execute code

1. Push the jenkinsfile to the remote warehouse

pipeline {
    
    
    agent {
    
    
        label 'Node_python_0702'
    }

    parameters {
    
    
        string(name: 'Version',defaultValue:'1.1.1’,description: '版本号')
        text name: 'msb',defaultValue: 'hello msb',description: 'hello msb'
        booleanParam(name: 'ifFlag',defaultValue: true, description: 'isFlag is True')
        choice name: 'choice',choices: ["java","python","测试"], description: 'choice class'
    }

    stages {
    
    
        stage('pull msbdemo') {
    
    
            steps {
    
    
                checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '28496d71-19df-4ba5-b7d4-8493cbf9d091', url: 'https://gitee.com/zhilongya/pytest_ci.git']])
            }
        }
        stage('build msbdemo') {
    
    
            steps {
    
    
                sh 'pip3 install -r requirements.txt'
                sh 'pytest -sv testcases --alluredir=./allure-results'
            }
        }
    }

}

insert image description here

2. Configure the pipeline

insert image description here
insert image description here

Special attention: the script path and the jenkinsfile file name in the remote warehouse should be consistent

3. The use of post in Jenkinsfile

1. Background and purpose

Background: Under normal circumstances, after we execute the corresponding code, we will do some post-build operations, such as generating a test report, sending emails and DingTalk, and other corresponding operations. If an error occurs during the build, no report will be generated, DingTalk will be sent, no email will be sent.
Purpose: Regardless of whether the build is successful, failed, or unstable, it is necessary to generate a test report, send DingTalk, and send an email.

2. Jenkinsfile file writing

pipeline {
    
    
    agent {
    
    
        label 'Node_python_0702'
    }

    parameters {
    
    
        string(name: 'Version',defaultValue:'1.1.1’,description: '版本号')
        text name: 'msb',defaultValue: 'hello msb',description: 'hello msb'
        booleanParam(name: 'ifFlag',defaultValue: true, description: 'isFlag is True')
        choice name: 'choice',choices: ["java","python","测试"], description: 'choice class'
    }

    stages {
    
    
        stage('pull msbdemo') {
    
    
            steps {
    
    
                checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '28496d71-19df-4ba5-b7d4-8493cbf9d091', url: 'https://gitee.com/zhilongya/pytest_ci.git']])
            }
        }
        stage('build msbdemo') {
    
    
            steps {
    
    
                sh 'pip3 install -r requirements.txt'
                sh 'pytest -sv testcases --alluredir=./allure-results'
            }
        }
    }
    post {
    
    
        always {
    
    
            allure includeProperties: false, jdk: '', results: [[path: 'allure-results']]

            sh 'cd "${WORKSPACE}"'
            sh 'python3 send_ding.py "${JOB_BASE_NAME}"'


            script{
    
    
                emailext body:
                '''  <!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="#FF0000">构建结果 - ${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><a href="${PROJECT_URL}">${PROJECT_URL}</a>
                            <td>
                                <ul>
                                    <li>项目名称:${PROJECT_NAME}</li>
                                    <li>GIT路径:<a href="[email protected]:wangxu/ops.git">[email protected]:wangxu/ops.git</a></li>
                                    <li>GIT分支:master</li>
                                    <li>构建编号:${BUILD_NUMBER}</li>
                                    <li>触发原因:${CAUSE}</li>
                                    <li>构建日志:<a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                                </ul>
                            </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><font color="#0B610B">Failed Test Results</font></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,escapeHtml=true}</textarea>
                            </td>
                        </tr>-->
                        <hr size="2" width="100%" align="center" />

                    </table>


                </body>
                </html>
''',
              subject: '$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS!', to: '[email protected]'
          }
        }
    }
}

3. Build a job

insert image description here

4. Send DingTalk

insert image description here

5. Send email

insert image description here

4. Post-build operation - the use of Post build task

InstallPlugin Post Build taskShell execution after generating the report

1. The operation of free style is as follows

insert image description here
insert image description here

insert image description here

2. Write using jenkinsfile

insert image description here
python3 send_ding.py "${JOB_BASE_NAME}": Pass the parameter "${JOB_BASE_NAME}" into the send_ding.py file. JOB_BASE_NAME: Indicates the job name

sys.argv[1] needs to be used in send_ding.py to receive
insert image description here

Guess you like

Origin blog.csdn.net/YZL40514131/article/details/131608579