Jenkins Pipeline: Input Step – Example

In Jenkins declarative pipelines it is possible to prompt a user for an interactive input by creating the input step.

For example, at some stage of the Jenkins pipeline you may want to ask a user to provide the credentials.

The user input can be saved as an environment variable and used in the next steps.

Bellow i will show an example of the Jenkins pipeline with the input step.

Cool Tip: How to define a variable in the Jenkins pipeline! Read more →

Jenkins Input Step Example

An example of the Jenkinsfile with the input step:

pipeline {
  agent any
  stages {
    stage ("Prompt for input") {
      steps {
        script {
          env.USERNAME = input message: 'Please enter the username',
                             parameters: [string(defaultValue: '',
                                          description: '',
                                          name: 'Username')]
          env.PASSWORD = input message: 'Please enter the password',
                             parameters: [password(defaultValue: '',
                                          description: '',
                                          name: 'Password')]
        }
        echo "Username: ${env.USERNAME}"
        echo "Password: ${env.PASSWORD}"
      }
    }
  }
}

In the example about we prompt a user for the input 2 times.

Each input step pauses the pipeline execution and waits for the user to type in the credentials:

[Pipeline] input
Input requested

Cool Tip: Decrypt secrets masked by Jenkins credentials plugin! Read More →

Once the input data is saved as environment variables, they can be printed to the console using the echo command:

[Pipeline] echo
Username: admin
[Pipeline] echo
Password: P@$$w0rd

One Reply to “Jenkins Pipeline: Input Step – Example”

  1. Just awesome script
    Thank you

Leave a Reply