2017-03-03 88 views

回答

1

不,流水線的正常流程是從頭到尾。 然而,你可以做的是測試你的成功狀態,而不是調用你的代碼的其餘部分,在一個如果或類似的東西。函數可以幫助您輕鬆實現,例如:

node() { 
    // Part 1 
    def isSuccess = part1(); 

    if(!isSuccess) { 
    part2() 
    } 
} 

// Part 2 
def function part2() { 
    // Part 2 code 
} 

然而,你應該小心那種東西,也許這更能突顯出您的管道設計不當。如果這不是您想要的,請提供更多詳細信息,例如用例。

0

首先,我不知道這樣的一個步驟。

但是,如果應該成功,您可以使用error步驟中止具有特定消息的構建。在try{}catch(){}塊中捕獲此錯誤,檢查消息並將構建狀態設置爲成功。

1

正如在其他答覆中所說的那樣,沒有以這種方式中止的步驟。正如克里斯托弗建議你可以在中止代碼周圍使用try-catch並使用error()。我認爲您需要跟蹤您的構建的中止狀態 - 您可以在管道中全局定義中止方法來設置此狀態併產生錯誤,以便放棄您舞臺中的其他步驟。

如果您使用了聲明式管道,則可以在後面的階段對錶達式使用'when'聲明,以便在設置中止狀態時不執行聲明。

我對這個問題自己,所以我制定了一個管道,這是否在這裏的一個例子:

/** 
* Tracking if the build was aborted 
*/ 
Boolean buildAborted = false 

/** 
* Abort the build with a message 
*/ 
def abortBuild = { String abortMessage -> 
    buildAborted = true 
    error(abortMessage) 
} 

pipeline { 

    agent any 

    parameters { 
     string(name: 'FailOrAbort', defaultValue: 'ok', description: "Enter 'fail','abort' or 'ok'") 
    } 

    stages { 

     stage('One') { 

      steps { 

       echo "FailOrAbort = ${params.FailOrAbort}" 

       script { 

        try { 

         echo 'Doing stage 1' 

         if(params.FailOrAbort == 'fail') { 

          echo "This build will fail" 
          error("Build has failed") 

         } 
         else if(params.FailOrAbort == 'abort') { 

          echo "This build will abort with SUCCESS status" 
          abortBuild("This build was aborted") 

         } 
         else { 

          echo "This build is a success" 

         } 

         echo "Stage one steps..." 

        } 
        catch(e) { 

         echo "Error in Stage 1: ${e.getMessage()}" 

         if(buildAborted) { 
          echo "It was aborted, ignoring error status" 
         } 
         else { 
          error(e.getMessage()) 
         } 
        } 
       } 
      } 

      post { 
       failure { 
        echo "Stage 1 failed" 
       } 
      } 
     } 

     stage('Two') { 

      when { 
       expression { 
        return !buildAborted 
       } 
      } 

      steps { 
       echo "Doing stage 2" 
      } 
     } 

     stage('Three') { 

      when { 
       expression { 
        return !buildAborted 
       } 
      } 

      steps { 
       echo "Doing stage 3" 
      } 
     } 
    } 

    post { 
     always { 
      echo "Build completed. currentBuild.result = ${currentBuild.result}" 
     } 
     failure { 
      echo "Build failed" 
     } 
     success { 
      script { 
       if(buildAborted) { 
        echo "Build was aborted" 
       } else { 
        echo 'Build was a complete success' 
       } 
      } 
     } 
     unstable { 
      echo 'Build has gone unstable' 
     } 
    } 
} 

作爲一個方面說明有一個屬性「currentBuild.result」你可以調整管道,但一旦設置爲'失敗'它不能被清除回'成功' - 詹金斯模型不允許它AFAIK

+0

用於描述'currentBuild.result'生命週期的榮譽 - 讓我不止一次地絆倒了我。 – rbellamy

相關問題