In this lab, we will see how you can capture interactive input in your Jenkins Pipeline while it is running by using the input directive and we will explore some pitfalls you will want to avoid when using the input
directive.
add-env-vars
branch of your copy of the insurance-frontend repository and update the Test stage
by adding the following between environment
directive and steps
block: input {
message "Should we continue with tests?"
}
We added an input
directive instead of an input
step in the steps
block. This ensures that the input
will prompt a user for input before the agent is used.
input
prompt for the Test
stage (the input
prompt is also available in the Console log). Go ahead and click the Proceed button and an agent will be provisioned and the Test
stage will run.input
prompt, the job would have waited indefinitely for an input
response. Let’s fix that by setting a timeout for the Test stage. We will add a timeout
option
for the Test stage
using the stage options directive (we will also remove the when
block). Update the Test stage
to match the following in the add-env-vars
branch and then commit the changes: stage('Test') {
agent any
options {
timeout(time: 10, unit: 'SECONDS')
}
environment {
FAVORITE_COLOR = 'BLUE'
SERVICE_CREDS = credentials('example-service-username-password')
}
input {
message "Should we continue with tests?"
}
steps {
sh 'echo TODO - test $FAVORITE_COLOR with SERVICE_CREDS: username=$SERVICE_CREDS_USR password=$SERVICE_CREDS_PSW'
}
}
stage
starts. Your pipeline will be automatically aborted 10 seconds after the Test stage
starts. CloudBees Pipeline Policies for CloudBees CI allows you to enforce the use of pipeline timeouts
across all pipeline jobs on a controller.
pipeline {
agent none
environment {
FAVORITE_COLOR = 'RED'
}
stages {
stage('Pull Request') {
when {
beforeAgent true
branch 'pr-*'
}
stages {
stage('Build and Push Container Image') {
steps {
echo "FAVORITE_COLOR is $FAVORITE_COLOR"
echo "TODO - Build and Push Container Image"
}
}
stage('Test') {
agent any
options {
timeout(time: 10, unit: 'SECONDS')
}
environment {
FAVORITE_COLOR = 'BLUE'
SERVICE_CREDS = credentials('example-service-username-password')
}
input {
message "Should we continue with tests?"
}
steps {
sh 'echo TODO - test $FAVORITE_COLOR with SERVICE_CREDS: username=$SERVICE_CREDS_USR password=$SERVICE_CREDS_PSW'
}
}
}
}
stage('Main Branch Stages') {
when {
beforeAgent true
branch 'main'
}
stages {
stage('Push Image to Prod Registry') {
steps {
echo "TODO - push image"
}
}
stage('Deploy') {
steps {
echo "TODO - deploy"
}
}
}
}
}
}