2017-04-10 55 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