Jenkins 嵌套阶段管道中的条件执行

时间:2021-04-12 16:17:54

标签: jenkins jenkins-pipeline

我已经看过 Conditional execution based on result of previous stage in declarative pipeline 并且我对这个问题有所了解:如果我们有一个简化的工作流程,比如 A-BC-{B1-B2-B3}-D 在顺序模式下? 我想要实现的是只有在 B1 成功时才执行阶段 B2,并且 B3 应该在 B1 失败的情况下执行,最后继续执行 D。 感谢您的投入。

1 个答案:

答案 0 :(得分:0)

以下确实对我有用 - 定义一个额外的地图
stageResultMap.ObjectdircreationFailed = true
在异常阶段并在应该在失败时调用的阶段中调用它.示例如下。

    stage ('Build'){

      stages {

        stage ('Objectdircreation'){
          steps {
            script {
                 try {
                  sh'''#!/usr/bin/env bash
                  PATH=/sdev/user/bin:/opt/rational/clearcase/bin:$PATH
                  export PATH
                  mkdir -p $OBJECTDIR
                  '''
                  stageResultMap.didObjectdircreationSucceed = true

              }
              catch (Exception e) {
                unstable("${STAGE_NAME} failed!")
                currentBuild.result = 'FAILURE'
                stageResultMap.didObjectdircreationSucceed = false
                //set an extra map to true and test it
                stageResultMap.ObjectdircreationFailed = true
              }
            }
          }
        } // end of Objectdircreation stage

        stage ('Inject objects under an objdir'){
          //
          // execute only if Objectdircreation succeeded
          //
          when {
            expression {
              return stageResultMap.find{ it.key == "didObjectdircreationcreationSucceed" }?.value
            }
          }
          steps {
            catchError(buildResult: 'FAILURE', stageResult: 'FAILURE')
              {
                  sh """#!/usr/bin/env bash

                  make ${ARGS} $OBJDIRPREFIX=$OBJDIR build
                  """
                   }
                 }

          } // end of build under an object tree

        stage ('Inject build under current objdir') {
        //
        // execute only if object dir creation failed
        //
         when {
           expression {
            return stageResultMap.find{ it.key == "didProjecttreecreationFailed" }?.value
           }
         }
           steps {
             catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
              sh """#!/usr/bin/env bash
              make ${ARGS} build
              """
            }

           }

          } //  end of build under root


        } // end of internal build stages
          //
          

    } // end of build nested stages

  } // end of main stages section
    //
    //
} //pipeline end