1

I've seen Conditional execution based on result of previous stage in declarative pipeline and I got a flavour of the question: how the things would look if we got a simplified workflow like A-BC-{B1-B2-B3}-D in sequential mode? What I'm trying to achieve is to have stage B2 executed only if B1 succeeds and B3 should be executed on condition B1 fails and finally proceed with D. Thanks for the input.

DIMV
  • 15
  • 3

1 Answers1

0

The following did work for me - define an extra map
stageResultMap.ObjectdircreationFailed = true
in the exception phase and call it in the stage which should be invoked upon failure. Example below.

    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
DIMV
  • 15
  • 3