Jenkins continuous delivery pipeline skip stage based on input

15,587

Solution 1

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"
}

Solution 2

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"
    }
}

Solution 3

How about:

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

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

Share:
15,587
lucas
Author by

lucas

Updated on June 22, 2022

Comments

  • lucas
    lucas almost 2 years

    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?