jenkins pipline異常捕獲


  1. timeout機制以及異常捕獲
https://e.printstacktrace.blog/how-to-time-out-jenkins-pipeline-stage-and-keep-the-pipeline-running/

pipeline {
    agent any
    options{
        timestamps()
    }
    stages {
        stage("A") {
            options {
                timeout(time: env.timeout, unit: "SECONDS")
                //MINUTES
            }

            steps {
                script {
                    Exception caughtException = null

                    catchError(buildResult: 'SUCCESS', stageResult: 'ABORTED') { 
                        try { 
                            echo "Started stage A"
                            sleep(time: 5, unit: "SECONDS")
                        } catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) {
                            error "Caught ${e.toString()}" 
                        } catch (Throwable e) {
                            caughtException = e
                        }
                    }

                    if (caughtException) {
                        error caughtException.message
                    }
                }
            }
        }

        stage("B") {
            steps {
                echo "Started stage B"
            }
        }
    }
}


  1. catchError 總體是failed,對應的stage也是failed,但是后面的stage還能正常執行
pipeline {
    agent any
    stages {
        
        stage('1') {
            steps {
                // build job:'test'
                echo 'in 1'
            }
        }
        
        stage('2') {
            steps {
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                    build job:'test'
                }
            }
        }
        
        stage('3') {
            steps {
               echo 'in 3'
            }
        }
        
        
    }
}



  1. try 總體的build是pass的,每個stage也是pass
pipeline {
    agent any
    stages {
        
        stage('1') {
            steps {
                 script {
                     try{
                          build job: 'test'
                      }    
                      catch (err){
                          echo "test failed"
                      }
                 }
            }
        }
        
        stage('2') {
            steps {
                 script {
                     try{
                          build job: 'test'
                      }    
                      catch (err){
                          echo "test failed"
                      }
                 }
            }
        }
        
        
        stage('3') {
            steps {
                 script {
                     try{
                          build job: 'test'
                      }    
                      catch (err){
                          echo "test failed"
                      }
                 }
            }
        }
        
        
    }
}


免責聲明!

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



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