How to Use Input in Jenkins Pipeline for User Interaction
In Jenkins Pipeline, use the
input step to pause the pipeline and wait for user interaction or approval. This step can prompt for confirmation or collect parameters before continuing the build process.Syntax
The input step syntax allows you to pause the pipeline and ask for user input. You can specify a message, parameters, and a timeout.
Key parts:
message: Text shown to the user explaining what input is needed.parameters: Optional list of inputs like strings, booleans, or choices.submitter: Optional user or group allowed to approve.timeout: Optional time limit to wait for input (in minutes).
groovy
input message: 'Approve deployment?', parameters: [string(name: 'VERSION', defaultValue: '1.0', description: 'Version to deploy')], submitter: 'admin', timeout: 10
Example
This example shows a simple Jenkins Declarative Pipeline that pauses to ask the user to approve deployment and enter a version number. The pipeline waits up to 5 minutes for input.
groovy
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Approval') {
steps {
script {
def userInput = input(
message: 'Approve deployment?',
parameters: [string(name: 'VERSION', defaultValue: '1.0', description: 'Version to deploy')],
timeout: 5
)
echo "User approved version: ${userInput}"
}
}
}
stage('Deploy') {
steps {
echo 'Deploying version...'
}
}
}
}Output
Building...
[Input requested: Approve deployment?]
User approved version: 1.0
Deploying version...
Common Pitfalls
Common mistakes when using input in Jenkins Pipeline include:
- Not wrapping
inputinside ascriptblock in Declarative Pipelines, causing syntax errors. - Forgetting to handle timeouts, which can cause the pipeline to hang indefinitely.
- Not specifying
submitter, which may allow unintended users to approve. - Using
inputin parallel stages without care, which can cause unexpected behavior.
groovy
pipeline {
agent any
stages {
stage('Approval') {
steps {
// WRONG: input outside script block in Declarative pipeline
input message: 'Approve?'
}
}
}
}
// Correct way:
pipeline {
agent any
stages {
stage('Approval') {
steps {
script {
input message: 'Approve?'
}
}
}
}
}Quick Reference
Tips for using input in Jenkins Pipeline:
- Always use
inputinside ascriptblock in Declarative Pipelines. - Use
parametersto collect user data. - Set
timeoutto avoid hanging builds. - Use
submitterto restrict who can approve. - Handle exceptions to manage user aborts gracefully.
Key Takeaways
Use the input step to pause Jenkins Pipeline and get user approval or data.
Wrap input inside a script block in Declarative Pipelines to avoid syntax errors.
Set timeout to prevent the pipeline from waiting forever.
Use parameters to collect specific input values from users.
Restrict approval permissions with the submitter option.