jenkins實現並發任務執行


jenkins並發編排

     jenkins並發工作流編排有利於提高執行效率 縮短構建時間

    設置不同stage段並發執行

#!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段內並發執行

   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超時返回結果

      timeout(時間設置,默認是分鍾為單位) {
          // 需要監控的代碼執行
       }

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

      使用try catch

      

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一樣返回成功

  啟動命令加nohup & 和不加nohup & 的區別

        

     

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM