2017-03-03 46 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

相关问题