jenkins实现并发任务执行

jenkins并发编排

     jenkins并发工作流编排有利于提高执行效率 缩短构建时间

    设置不同stage段并发执行

jenkins实现并发任务执行
#!groovy

@Library("myjenkinslib@master") _ 

def mytools = new org.tools()

pipeline {
   agent any
   
   stages {
      stage("Deploy Service"){
        steps {   
            sh "ansible webservers2  --user=admin -m copy -a 'src=${srcPath}/admin/target/admin-prod.jar dest=/app/chuangfa/taishi/app/admin/'"
            }
      }
      
    stage('start backen and front') {
     parallel {
     stage("Start backend"){
        steps {
         script {
           res = sh(script: "ansible webservers2 --user=admin -m shell -a 'sudo supervisorctl restart admin'", returnStatus: true)
           if(res != 0){
                error("admin服务启动失败,本次发布流程终止")
           }
          }
         }
              
        post {
             success {
                print("所有应用启动成功,本次后台流水线执行成功")
              }
        }
    }
    
    stage("Start front"){
        steps {
             script {
              res = sh(script: "ansible webservers2 --user=admin -m shell -a 'sudo supervisorctl restart sdces'", returnStatus: true)
              if(res != 0){
              error("sdces服务启动失败,本次发布流程终止")
             }
            }
              
        }
        post {
             success {
                print("所有应用启动成功,本次后台流水线执行成功")
              }
        }
    }
   }
  }
  }
}
pipeline

   同一个stage段内并发执行

jenkins实现并发任务执行
   stage('start service') {
        steps {
          parallel(
              a:{ 
                script {
                 res = sh(script: "ansible webservers2 --user=admin -m shell -a 'sudo supervisorctl restart admin'", returnStatus: true)
                 if(res != 0){
                  error("admin服务启动失败,本次发布流程终止")
                 }
               }
               },
              b:{ 
                  script {
                    res = sh(script: "ansible webservers2 --user=admin -m shell -a 'sudo supervisorctl restart sdccollect'", returnStatus: true)
                     if(res != 0){
                        error("sdccollectconfig服务启动失败,本次发布流程终止")
                     }
                   }
               },
              c:{ 
                 script {
                    res = sh(script: "ansible webservers2 --user=admin -m shell -a 'sudo supervisorctl restart sdcrule'", returnStatus: true)
                    if(res != 0){
                    error("sdc-rule-config服务启动失败,本次发布流程终止")
                   }
                  }  
               },
              d:{ 
                 script {
                  res = sh(script: "ansible webservers2 --user=admin -m shell -a 'sudo supervisorctl restart sdces'", returnStatus: true)
                  if(res != 0){
                     error("sdces服务启动失败,本次发布流程终止")
                 }
                 }
               }
          )
        
        }
        post {
             success {
                print("所有应用启动成功,本次后台流水线执行成功")
              }
        }
    
  }
pipeline

  parallel不能嵌套parallel

       jenkins实现并发任务执行

 并发执行效率提升效果

 jenkins超时返回结果

      timeout(时间设置,默认是分钟为单位) {
          // 需要监控的代码执行
       }

       options { timeout(time: 1, unit: 'HOURS') }

      使用try catch

      jenkins实现并发任务执行

jenkins实现并发任务执行
pipeline {
    agent any
   
    options {
        timeout(time: 5)
    }
    
   environment {
    destPath="C:/PythonScriptTest"
   }

   stages {
   
      stage('GetCode'){
         steps {
            // Get some code from a GitHub repository
            git credentialsId: 'yxhgitlab', url: 'https://192.168.30.3331:8090/hfm/automation.git'

            // To run Maven on a Windows agent, use
            // bat "mvn -Dmaven.test.failure.ignore=true clean package"
         }

         post {
            // If Maven was able to run the tests, even if some of the test
            // failed, record the test results and archive the jar file.
            success {
              print("getCode success")
            }
         }
      }
      
      stage("Deploy"){
           steps {
              script {
                 print("Deploy success......")
                 sh "sudo ansible windows -m win_copy -a 'src=/var/lib/jenkins/workspace/pipeline-test1/ dest=${destPath}/'"
              }
           }
           post {
              success {
                print("Deploy success......")
              }
           }
      }
      
      stage("Start"){
         steps {
            script {
               try {
                   
                     timeout(2) {
                       sh "sudo ansible windows -m win_command -a 'chdir=${destPath}/Web  python runall.py'"
                     }
               }
               catch (exc) {
                 print("runall.py已被成功启动,稍后会自动生成测试报告!")
               }
              }
           }
        }
    }
}
pipeline

  即使发生timeout异常 但是由于使用try语句 pipeline一样返回成功

上一篇:jenkins pipeline


下一篇:寒假记录3