Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a stage status in Jenkins Pipelines

Is there any way in a scripted pipeline to mark a stage as unstable but only show that stage as unstable without marking every stage as unstable in the output?

I can do something like this:

node()
{
  stage("Stage1")
  {
      // do work (passes)
  }
  stage("Stage2")
  {
      // something went wrong, but it isn't catastrophic...
      currentBuild.result = 'UNSTABLE'
  }
  stage("Stage3")
  {
      // keep going... 
  }
}

But when I run this, Jenkins marks everything as unstable... but I'd like the first and last stages to show green if possible and just the stage that had an issue to go yellow.

It's ok if the whole pipeline gets flagged unstable, but it might also be nice to have a later stage over ride that and set the final-result to pass if possible too.

like image 831
TxAG98 Avatar asked Jan 26 '18 23:01

TxAG98


3 Answers

This is now possible:

pipeline {
    agent any
    stages {
        stage('1') {
            steps {
                sh 'exit 0'
            }
        }
        stage('2') {
            steps {
                catchError(buildResult: 'SUCCESS', stageResult: 'UNSTABLE') {
                    sh "exit 1"
                }
            }
        }
        stage('3') {
            steps {
                sh 'exit 0'
            }
        }
    }
}

In the example above, all stages will execute, the pipeline will be successful, but stage 2 will show as unstable. I use a declarative pipeline in the example, but it should work the same in a scripted pipeline.

As you might have guessed, you can freely change the buildResult and stageResult to any combination. You can even fail the build and continue the execution of the pipeline.

Just make sure your Jenkins is up to date, since this is a fairly new feature. Upgrade any plugins affected by bug JENKINS-39203, such as:

  • Pipeline Graph Analysis plugin (at least version 1.10 to mark single stages as 'UNSTABLE')
  • Pipeline Basic Steps plugin (at least version 2.18 to set stageResult in catchError)
like image 119
Erik B Avatar answered Oct 12 '22 13:10

Erik B


Also worth mentioning are the warnError and unstable steps, released on Jul/2019, as part of the "Jenkins Pipeline Stage Result Visualization Improvements".

They are meant to allow you to mark a stage as unstable (this appears as an amber warning icon, and not a red error icon), while the rest of the build is still marked as successful.

Examples (lifted from the link above):

warnError acts like catchError, and changes the stage to have a warning if any part of the block fails:

warnError('Script failed!') {
  sh('false')
}

unstable is a directive-style step, which you can use to mark the current stage as unstable:

try {
  sh('false')
} catch (ex) {
  unstable('Script failed!')
}
like image 33
Shalom Craimer Avatar answered Oct 12 '22 15:10

Shalom Craimer


an elegant way I found to set only the stage result but not change the build result is this:

catchError(stageResult: 'UNSTABLE', buildResult: currentBuild.result) {
     error 'example of throwing an error'
}
like image 23
Krakowski mudrac Avatar answered Oct 12 '22 14:10

Krakowski mudrac