Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mark a stage in Jenkins Pipeline as eg "UNSTABLE" but proceed with future stages?

I'm going to use Jenkins pipeline plugin to test several binaries A B C on several nodes 1 2 3. In the end of my test I would like to have every single result of all possible combinations. So my Pipe may not abort when a single stage fails. It should proceed.

eg: A1 green, A2 green, A3 red, B1 green, B2 red, ..., C3 green

But when the first binary returns with an value unequal zero ("Binary not working on the system") it's stage is marked as FAILURE and any other stages are skipped.

Is there a possibility in Jenkins Pipeline to mark a stage as "UNSTABLE" but proceed with running the other tests?

According to Continue Jenkins job after failed stage while marking stage as failed can't mark this step as failed. The solution of this in running tasks in parallel is not working for my setup. So is it possible to safely mark it as something else? Is it possible to manipulate the result of a stage?

This question How to continue past a failing stage in Jenkins declarative pipeline syntax intents to use a scripted pipeline. I would like to avoid that if it is possible to do it in an other way.

pipeline {
    agent {label 'master'}     
     stages {            
        stage('A1') { 
            agent {label 'Node1'} 
            steps {
                sh 'binA'
            }
        }
        stage('A2') {
            agent {label 'Node1'}
            steps {
                sh 'binB' // If this bin fails, all following stages are skipped
            }
        }
// ...        
        stage('C3'){
            agent {label 'Node3'}
            steps {
                sh 'binC'
            }
        }
    }
}

like image 867
Cutton Eye Avatar asked Dec 03 '17 16:12

Cutton Eye


3 Answers

Declarative Pipeline: Though using currentBuild.result = 'UNSTABLE' works in declarative pipelines too, Blue Ocean displays all stages as unstable irrespective of which stage fails.

enter image description here

To mark only specific stages as unstable, use the step unstable(message: String) as described here within your stage and install/update the following plugins:

  • Pipeline: Basic Steps to 2.16 or newer
  • Pipeline: API Plugin to 2.34 or newer
  • Pipeline: Groovy to 2.70 or newer
  • Pipeline Graph Analysis to 1.10 or newer

Sample pipeline stage:

stage('Sign Code') {
    steps {
        script {
            try {
                pwd()
                sh "<YOUR SCRIPT HERE>"
            }
            catch (err) {                                        
                unstable(message: "${STAGE_NAME} is unstable")
            }
        }
    }
}

enter image description here

Note: This also marks the overall build status as unstable.

like image 195
Dibakar Aditya Avatar answered Sep 17 '22 11:09

Dibakar Aditya


There is now a more elegant solution, that not only allows you to set a stage and the job result to unstable. Using catchError, you can set any combination of stage and build result:

pipeline {
    agent any
    stages {
        stage('1') {
            steps {
                sh 'exit 0'
            }
        }
        stage('2') {
            steps {
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                    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 failed:

Pipeline Example

As mentioned above, you can freely choose the buildResult and stageResult. 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. (Pipeline: Basic Steps needs to be 2.18 or newer)

like image 27
Erik B Avatar answered Sep 17 '22 11:09

Erik B


For scripted pipeline, you can use try .. catch blocks inside the stages and then set currentBuild.result = 'UNSTABLE'

in the exception handler.

like image 30
Bernhard Cygan Avatar answered Sep 16 '22 11:09

Bernhard Cygan