2017-04-10 57 views
1

我嘗試使用並行步驟嘗試發佈失敗操作,但它無法工作。發佈失敗JenkinsFile無法正常工作

這是我JenkinsFile:

pipeline { 
    agent any 

    stages { 

     stage("test") { 

      steps { 

       withMaven(
          maven: 'maven3', // Maven installation declared in the Jenkins "Global Tool Configuration" 
          mavenSettingsConfig: 'maven_id', // Maven settings.xml file defined with the Jenkins Config File Provider Plugin 
          mavenLocalRepo: '.repository') { 
           // Run the maven build 
           sh "mvn --batch-mode release:prepare -Dmaven.deploy.skip=true" --> it will always fail 
          }  
      } 
     } 

     stage("testing") { 
      steps { 
       parallel (
        phase1: { sh 'echo phase1'}, 
        phase2: { sh "echo phase2" } 
        ) 
      } 
     } 

    } 

    post { 

     failure { 

      echo "FAIL" 
     } 
    } 
} 

但這裏的失敗後動作是有點useles ......我不看它的任何地方。

謝謝大家! Regards

+1

我有完全相同的問題!對此有幫助嗎? – Alan47

回答

3

我發現了這個問題,經過幾個小時的搜索。你錯過了什麼(我也錯過了)是catchError部分。

pipeline { 
    agent any 
    stages { 
     stage('Compile') { 
      steps { 
       catchError { 
        sh './gradlew compileJava --stacktrace' 
       } 
      } 
      post { 
       success { 
        echo 'Compile stage successful' 
       } 
       failure { 
        echo 'Compile stage failed' 
       } 
      } 
     } 
     /* ... other stages ... */ 
    } 
    post { 
     success { 
      echo 'whole pipeline successful' 
     } 
     failure { 
      echo 'pipeline failed, at least one step failed' 
     } 
    } 

您應該將可能失敗的每一步都包裝到catchError函數中。這樣做是:

  • 如果發生錯誤...
  • ...設置build.resultFAILURE ...
  • ...和繼續構建

的最後一點很重要:你的post{ }塊沒有被調用,因爲你的整個管道是中止,他們甚至沒有機會執行。

+0

這種情況下的問題是平行步驟。如果你使用沒有平行的正常舞臺。所有的帖子操作都很好。 –

+0

不適合我。我的構建管道中沒有並行性,如果其中一個步驟中的shell腳本未成功執行,則拒絕發佈後操作。我實際上必須使用'catchError'來查看'post {}'動作的結果。 – Alan47