Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins continuous delivery pipeline skip stage based on input

A simplified pipeline will look something like:

 1. build
 2. unit test
 3. deploy to dev
 4. integration tests
 5. deploy to prod

For step #5 I've setup a Jenkins pipeline input command. We won't be deploying to prod on every commit so if we abort all those jobs it will have a big list of grey builds. Is it possible to have a skip option so the build can still be shown as green blue?

like image 241
lucas Avatar asked Nov 30 '16 19:11

lucas


People also ask

How do I skip a particular stage in Jenkins?

You can skip stages in declarative pipelines using when , so the following should work. stages { stage('Deploy') { when { equals expected: true, actual: Deploy } steps { // ... } } } If it should be totally invisible in the review pipeline, then use scripted pipelines and wrap the stage with an if statement.

How do you skip stage in Jenkins scripted pipeline?

The way to do this is by using Jenkins' module that can be found here. So to mark a stage as skipped you need to call static method markStageSkippedForConditional passing the name of the stage you are skipping.

How do you skip failed stage in Jenkins pipeline?

To ignore a failed step in declarative pipeline you basically have two options: Use script step and try-catch block (similar to previous proposition by R_K but in declarative style)

How do I skip a build in Jenkins?

use the "Set the build result" step to set the status to "Aborted", this will finish the Jenkins build and will hide the build from the Jenkins dashboard.


3 Answers

There is a better solution I just found. You can access the result of the input like by using the return value. The user has to check the checkbox, to run the optional stage. Otherwise the steps of the stage are skipped. If you skipp the whole stage, the stage will disappear and that "cleans" the stage view history.

stage('do optional stuff?') {
    userInput = input(
        id: 'userInput', message: "Some important question?", parameters: [
        booleanParam(defaultValue: false, description: 'really?', name: 'myValue')
    ])
}

stage('optional: do magic') {
    if (userInput) {
        echo "do magic"
    } else {
        // do what ever you want when skipping this build
        currentBuild.result = "UNSTABLE"
    }
}
like image 51
Matthias B Avatar answered Oct 22 '22 11:10

Matthias B


How about:

stage('Deploy') {
  when { branch 'master' }
    steps {
      sh '...'
    }
  }
}

the stage will be skipped for commits on other branches and will be green.

like image 43
Wojciechem Avatar answered Oct 22 '22 12:10

Wojciechem


Can't you do something like this, it will be blue/green whatever you choose from input, and you can then run the deployment depending on it too?

def deployToProduction = true
try{
  input 'Deploy to Production'
}catch(e){
   deployToProduction = false
}

if(deployToProduction){
    println "Deploying to production"
}
like image 2
MaTePe Avatar answered Oct 22 '22 11:10

MaTePe