Dileeka Fernando
Dileeka Fernando

Reputation: 211

Jenkins Pipeline stage skip based on variable

I am trying to get the user input and execute following Jenkins stage once RUN_BACKUP parameter is set to "YES" . However when RUN_BACKUP parameter is set to "NO" . it always ask user input. How can I avoid it and get the user input when RUN_BACKUP parameter is set to "YES"

    stage('Executing DB backup stage') {
  when {
     expression { params.RUN_BACKUP == 'YES' }
  }
    input {
      message "Enter DB parameters?"
      ok "OK"
      parameters {
        string(defaultValue: '', name: 'DB_HOST', description: 'database host')
        string(defaultValue: '30051', name: 'DB_PORT', description: 'database port')
        password(defaultValue: '', name: 'DB_PASSWORD', description: 'database password')
      }
    }  
  steps {
     script {
      def now = LocalDateTime.now()
      def datePart = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
      print "Current Date is : ${datePart}"
      def statusCode = sh(script: "run my own backup command", returnStatus:true)
      println "Exit Status = ${statusCode}"
      if(statusCode != 0){
        error "Error Backing Up Database"
      }
      else {
        print "Do that"

      }
   }
  }
}

Upvotes: 0

Views: 692

Answers (1)

Catalin
Catalin

Reputation: 484

Check the beforeInput parameter of the when directive.

Upvotes: 1

Related Questions