Yiwen will automate the construction of jenkins pipline

01 Basic syntax of Pipeline

First create a pipeline task that creates a pipeline on jenkins

New ITEM->Select Pipeline
insert image description here

Basic pipeline script structure

pipeline {

//agent 表示要执行的节点,any表示任意节点   

agent any

//stages represents a collection of all steps when the task is executed

stages {

     //stage就表示一个步骤,括号里是步骤名称       

     stage('拉取项目源码'){           

           //每一个stage都可以定义自己执行的节点,如果没定义,则用最上方的           

 agent {

            // label 后跟的是节点的标签名称

            label 'mall-server'

   }

        steps {

            echo '这是拉取代码这一步'

            echo "Running ${env.BUILD_ID} on ${env.JENKINS_URL} ${env.JOB_NAME}"

            echo "${currentBuild.result} sdsdd"

            sh 'pwd'

            //如果当前节点是windows,我想执行windows下的命令

            //bat 'dir'

         }

        

     }

    

    stage('静态代码扫描'){

        steps {

            echo '这是静态代码扫描'

        }

    

        

    }

    

    stage('单元测试'){

        steps {

            echo '执行jacoco单元测试'

        }           

    }

    

    stage('打包依赖服务'){

        steps {

            echo '打包依赖服务'

        }           

    }

    

    stage('打包当前服务'){

        steps {

            echo '打包当前服务'

        }           

    }

    

    stage('部署环境'){

        steps {

            echo '部署环境'

        }           

    }

    

    stage('接口自动化测试'){

        agent {

            label 'auto_test'

        

        }

        steps {

            echo '接口自动化测试'

        }      

    }

    stage('ui自动化'){

        steps {

            echo 'ui自动化'

        }       

        

    }

}

}

02 Write a pipeline script for the mall microservice project

For the pipeline script, we don't need to remember so many things, we can open our own pipeline task, click the pipeline syntax, and use the following two menus to help us generate some scripts
insert image description here

For a project, there may be multiple services and multiple environments.

So we need to create environment node parameters, service name parameters
insert image description here

Add the following at the top of the pipeline script:

//Define two sets of option parameters
parameters { choice choices: ['mall-admin', 'mall-demo', 'mall-auth', 'mall-serach'], name:'servername' choice choices: ['mall -server', 'mall-dev', 'mall-prod'], name: 'node_env' }


After the configuration is complete, perform a task first

Select the node where the project is deployed

Because the deployed node environment is based on the parameters of the previous step, we will execute whatever the user chooses, so the value of label is a variable

agent {

    label "${node_env}"

}

Pull project code
insert image description here

Copy the generated script and put it under steps in the stage of pulling the code

stage('Pull project source code'){

 steps {  

       echo '这是拉取代码这一步'  

       git credentialsId: '4c1003ef-ac70-4271-8649-1759e79d430a', url: 'http://192.168.0.109/shamo/mall-swarm-master.git' 

  }  

}

unit test coverage

Since the mall project originally did not integrate jacoco-related coverage statistics, we first made some modifications to the project

① Modify the pom.xml file at the root of the entire project

false

② Modify the pom.xml of each submodule service

org.jacoco
  <artifactId>jacoco-maven-plugin</artifactId>     

  <version>0.8.6</version>
 <groupId>org.jacoco</groupId>   

 <artifactId>jacoco-maven-plugin</artifactId>  

 <version>0.8.6</version>   

 <configuration>       

       <destFile>target/coverage-reports/jacoco-unit.exec</destFile>

       <dataFile>target/coverage-reports/jacoco-unit.exec</dataFile>

  </configuration>  

  <executions>      

      <execution>           

          <id>jacoco-initialize</id>    

          <goals>               

               <goal>prepare-agent</goal>                      

          </goals>      

      </execution>

      <execution>           

         <id>jacoco-site</id>        

        <phase>test</phase>           

         <goals>              

            <goal>report</goal>           

         </goals>       

      </execution>   

  </executions>

③ Write a pipeline
insert image description here

Copy the generated jacoco instruction and fill in the corresponding steps

stage('unit test'){

   steps {           

        echo '执行jacoco单元测试'           

        sh '''           

        cd "${servername}"           

        mvn test            

        '''           

        jacoco changeBuildStatus: true,

        maximumBranchCoverage: '100',

        maximumClassCoverage: '100',

        maximumComplexityCoverage: '100',

        maximumInstructionCoverage: '100',

        maximumLineCoverage: '100',

        maximumMethodCoverage: '100',

        minimumBranchCoverage: '90',

        minimumClassCoverage: '90',

        minimumComplexityCoverage: '90',

        minimumInstructionCoverage: '90',

        minimumLineCoverage: '90',

        minimumMethodCoverage: '90'       

    }              

}

Static code scanning
insert image description here

Copy the generated ones to the steps of the static scan stage, and then add the sonar scan instructions

stage('Static code scan'){

steps { 

     echo '这是静态代码扫描'  

     withSonarQubeEnv(credentialsId: '1d74c7e8-5b27-4772-9a21-41e17eb87b7d', installationName: 'sonar') {

          // 执行sonar扫描的指令   

          sh '''   

          cd "${servername}"   

          mvn sonar:sonar \   

          -Dsonar.projectKey=${JOB_NAME}-${servername}$BUILD_NUMBER \   

          -Dsonar.projectName=mall \   

          -Dsonar.language=java \   

          -Dsonar.sourceEncoding=UTF-8 \   

          '''  

       } 

 }

}

package and deploy

stage('Service package and deploy'){

  steps {   

       echo '打包依赖服务'   

       //打包每个服务都依赖的模块   

       sh 'mvn clean install -pl mall-common,mall-mbg -am'   

       //打包我要部署的这个服务模块   

       sh '''   

       cd "${servername}"   

       mvn clean package   

       '''    

       //执行部署脚本文件  

       sh '/mydata/sh/${servername}.sh'  

  }    

}

Dingding notification

DingTalk notifications can actually be added at each stage, we load it into the deployment

stage('Service package and deploy'){

steps {  

    echo '打包依赖服务'  

    //打包每个服务都依赖的模块  

    sh 'mvn clean install -pl mall-common,mall-mbg -am'  

    //打包我要部署的这个服务模块  

    sh '''  

    cd "${servername}"  

    mvn clean package  

    '''   

    //执行部署脚本文件  

    sh '/mydata/sh/${servername}.sh'  

    //部署完成后钉钉通知项目组  

   dingtalk(   

       robot:'dd001',//robot指的是你在系统配置中配的钉钉机器人的id   

       type:'MARKDOWN',   

       atAll: false,   

       title: "${servername} 部署成功",   

       text: ["#### '${JOB_NAME}'项目扫描部署  \n - 任务:第'${BUILD_NUMBER}'次\n - 状态:'${currentBuild.result}' \n - 执行人: '${user}' \n \n[查看控制台]('${BUILD_URL}')"]  

    ) 

}

}

Configure automated tasks

The script of the allure plugin is generated as follows:
insert image description here

The script for the email is generated as follows:
insert image description here

stage('Interface automation test'){

     //如果之前的步骤执行时失败了改动了当前任务的结果,那么自动化测试没有必要执行  

     //下面的判断指的是没结果时或者结果是成功时

     when {       

         expression {      

           currentBuild.result==null||currentBuild.result == 'SUCCESS'        

         }     

      }  

      //使用自动化执行的节点  

      agent {   

          label 'auto_test'     

      }  

      steps {   

         echo '接口自动化测试'   

         //拉取自动化脚本   

         git credentialsId:'4c1003efac70-4271-8649-1759e79d430a', url: 'http://192.168.0.109/shamo/mallapitest1.git'   

         //执行自动化测试,由于这个节点是windows的,所以采用bat xxx来执行命令操作   

         bat 'python run.py'  

         //生成测试报告   

         allure includeProperties: false, jdk: '', results: [[path: 'report/shop']]   

         //发送结果邮件   

         emailext body: '''<!DOCTYPE html>  

              <html>      

              <head>      

              <meta charset="UTF-8"> 

              <title>$PROJECT_NAME-第$BUILD_NUMBER次构建日志</title>      

              </head>      

      

              <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"    offset="0">

                 <div> 

                 <table width="95%" cellpadding="0" cellspacing="0"    style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">       

                    <tr>       

                      <th colspan="2"><br />        

                           <h2>构建信息</h2>        

                      </th>      

                    </tr>      

                    <tr>         

                        <td>          

                           <ul>   

                              <li>项目名称 :$PROJECT_NAME</li><br />          

                              <li>详细测试日志 :<a href=${BUILD_URL}console target=\'_blank\'>${BUILD_URL}console</a></li><br />         

                              <li>详细测试报告 :<a href=${JOB_URL}${$BUILD_NUMBER}/allure target=\'_blank\'>${JOB_URL}${$BUILD_NUMBER}/allure</a></li><br />         

                              <li>触发原因:${CAUSE}</li><br />          
                              <li>项目  Url :<a href=‘$BUILD_URL’ target=\'_blank\'>$BUILD_URL</a></li><br />        

                           </ul>         

                       </td>       

                    </tr>       

               </table>      

            </div>      

       </body>      

    </html>''',     

 subject: '$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS!',        to: '[email protected]'      

}

}

final execution

insert image description here

Chinese online documentation: https://www.jenkins.io/zh/doc/book/pipeline/
and one more: https://www.w3cschool.cn/jenkins/jenkins-qc8a28op.html

Guess you like

Origin blog.csdn.net/Testfan_zhou/article/details/124099540