Search code examples
dockerjenkinsgroovy

Jenkins stage exited with code 1 and passing stage


in my pipeline one of the stage is exited with code 1 and still pipeline is passing. I want to fail the pipeline stage with exception when it is exiting with code 1.

Here is stage code:

stage('Stage A'){
  steps{
    script{
      container('A'){
        sh "$Command"
                def exitCode = sh 'echo $?'
                if (exitCode == 1) {
                     sh "exit 1"
        }
      }
    }
  }
}

Screenshot link


Solution

  • If you want to error out the Pipeline you can use error signal.

    def exitCode = sh script:"$Command", returnStatus:true
    if (exitCode == 1) {
         error "Exit code is 1, so erroring out!"
    }