Ashar
Ashar

Reputation: 3249

Need Condition based Input Step in Jenkins pipeline script

I have three stages in jenkins pipeline script viz (1) precheck, (2) build-prod & (3) build-dr.

I have stated the input step for manual trigger at stage "build-dr"

My pipeline is condition based i.e based on user parameter in precheck stage .

Condition1: "precheck" -> "build-prod" and then "build-dr" is executed.

Condition2: "precheck" and then "build-dr" is executed (skips build-prod).

I need the input step in Condition1 and is working fine however the input step should not be executed for Condition2 i.e no popup msg with input step. Please let me know how can we put a condition around input step in stage 3 build-dr so it does not execute input step when the pipleline skips (2) build-prod.

Jenkins pipeline Script Code:

  agent any         
  stages {
      stage ("Pre-Check Parameters") {      
          steps {

              echo "Pre-Check called in pipeline"
          }
        }
              stage ("build-prod") {           
          when {
              expression { params.region == 'prod_only' || params.region == 'prod_and_dr' }
          }
          steps { 
              build 'job4'
          }

        }
          stage ("build-dr") {           
                  input{
                  message "Proceed or Abort"
                  submitter "user1,admin"
                  parameters {
                  string(name:'username', defaultValue: 'user1', description: 'Username of the user pressing Ok')
                  }
              } 

          when {
              expression { params.region == 'dr_only' || params.region == 'prod_and_dr'}
          }
          steps { 
              build 'job5'
          }    
      }
  }
}

Kindly suggest.

Upvotes: 0

Views: 5416

Answers (1)

Jns
Jns

Reputation: 3257

You are currently using the input directive as described here but this prevents you to make this input conditional. You actually have to use the Input Step. Instead of adding the input field directly below the stage directive you move this into the steps block of your stage and add a script block around it to use if/else conditionals.

And take care to remove the curly brackets around you input step and to add a colon after each property.

What you have to do now is to adapt this line to your requirements: if(Condition1 == true) Depenending on the value of your parameter.

stage("build-dr") {
    when {
        expression { params.region == 'dr_only' || params.region == 'prod_and_dr'}
    }
    steps {
        script {
            if(Condition1 == true) {
                input message: "Proceed or Abort", submitter: "user1,admin",
                parameters: [string(name:'username', defaultValue: 'user1', description: 'Username of the user pressing Ok')] 
            }
        }
        build 'job5'
    }
}

Alternatively you can use an environment declaration block to declare a variable and assign a specific value to it if your second stage will be executed. But any environment value will always be typed as a String this is important for the if/else conditional. A good way to assign a value to the variable would be to add a post section to your second stage.

pipeline {
    agent any
    environment {
        STAGE_2_EXECUTED = "0"
    }
    stages{
        stage("....") {
            steps {
                ....
            }
        }
        stage("Second stage") {
            when {
                expression { /* Your condition */ }
            }
            steps {
                ....
            }
            post {
                always {
                    script {
                        STAGE_2_EXECUTED = "1";
                    }
                }
            }
        }
        stage("Third Stage") {
            steps {
                script {
                    if(STAGE_2_EXECUTED == "1") {
                        input message: "Proceed or Abort", submitter: "user1,admin",
                        parameters: [string(name:'username', defaultValue: 'user1', description: 'Username of the user pressing Ok')] 
                    }
                }
                build 'job5'
            }
        }
    }
}

Upvotes: 2

Related Questions