In this exercise, we will see how you can capture interactive input in your Jenkins Pipeline while it is running.
- Use the GitHub file editor to update the Jenkinsfile file in the master branch of your forked helloworld-nodejs repository - adding the following
stage
to your Pipeline after the Build and Push Imagestage
and commit the changes:
stage('Deploy') {
when {
beforeAgent true
beforeInput true
branch 'master'
}
input {
message "Should we continue?"
}
steps {
echo "Continuing with deployment"
}
}
|NOTE: We added the the beforeInput
option set to true
so that the when
condition will be checked before the input
- if we didn't add that option then the input
would be executed before the when
condition and it would execute for all branches, not just the master
branch.
-
Navigate to the helloworld-nodejs job in Blue Ocean on your Team Master and the job for the master branch should be running or queued to run. Note the
input
prompt during theDeploy
stage. Go ahead and click the Proceed button and the job will complete successfully. Theinput
prompt is also available in the Console log and classic Stage View. -
If you hadn't clicked on either the Proceed or Abort button in the
input
prompt then your Team Master would haved waited indefinitely for a user response. Let's fix that by setting a timeout. Earlier we usedoptions
at the globalpipeline
level to set the Discard old builds strategy for your Team Master with thebuildDiscarder
option
. Now we will configureoptions
at thestage
level. We will add atimeout
option
for the Deploystage
using thestage
options
directive. Update the Deploystage
to match the following in the master branch and then commit the changes:
stage('Deploy') {
when {
beforeAgent true
beforeInput true
branch 'master'
}
options {
timeout(time: 30, unit: 'SECONDS')
}
input {
message "Should we continue?"
}
steps {
echo "Continuing with deployment"
}
}
- Navigate to the helloworld-nodejs job in Blue Ocean and wait at least 30 seconds after the 'Deploy'
stage
starts. Your pipeline will be automatically aborted 30 seconds after the 'Deploy'stage
starts.Run it again if you would like - but this time click the Proceed button before 30 seconds expires - the job will complete successfully.
The input
directive supports a number of interesting configuration options. In this exercise we are going to use the submitter
option to control what Team Master member is allowed to submit the input
directive. But first you need to add another member to your CloudBees Team Master. Team Masters provide an easy to use authorization model right out-of-the-box. The following roles are available (there is a CLI to add or modify roles):
- Team Admin: administrator of the Team Master.
- Team Member: read, write and execute permission on the pipelines.
- Team Guest: read only.
We want to add a Team Guest to our Team Masters and then set that Team member as the submitter
for our input
directive. Before you begin, pick a person near you to pair up with. The two of you will share each other's Jenkins account names. You will use that account name when adding a new member to your Team Master below:
- On your Team Master, navigate to the Team list by clicking on the Administration link on the top right (this link is available on all Blue Ocean pages except for the Pipeline Run Details view).
- Next, click on the cog icon for your team.
- Click on the Members link in the left menu and then click on the Add a user or group link.
- Select Team Guest from the role drop-down, enter the account name for the person next to you in the Add user or group input (I will use beedemo-ops), press your enter/return key, and then click the Save changes button.
- Click on the Pipelines link in the top menu.
Now that we all have a new team member, you can add them as a submitter
for the input
directive in your Jenkinsfile
Pipeline script.
- Use the GitHub file editor to update the Jenkinsfile file in the master branch of your forked helloworld-nodejs repository - updating the
input
directive of the Deploystage
with the following changes (replacing beedemo-ops with Jenkins username of your new Team Guest member). Also, update thetimeout
duration to give your approver plenty of time to submit theinput
:
options {
timeout(time: 60, unit: 'SECONDS')
}
input {
message "Should we deploy?"
submitter "beedemo-ops"
submitterParameter "APPROVER"
}
- So, we added one additonal configuration option for our
input
directive:submitterParameter
. Setting thesubmitterParameter
option will result in a Pipeline environmental variable namedAPPROVER
being set with the value being the username of the user that submitted theinput
. In the example above it will be beedemo-ops. Update thesteps
section so theecho
step in yourJenkinsfile
Pipeline script will print theAPPROVER
environmental variable and then commit the changes:
steps {
echo "Continuing with deployment - approved by ${APPROVER}"
}
- Navigate to the master branch of your helloworld-nodejs job in Blue Ocean on your Team Master. The job should be waiting for
input
: - The submitter needs to navigate to the master branch of your helloworld-nodejs job on your Team Master to approve the
input
of your helloworld-nodejs Pipeline. You can use the Team switcher to quickly navigate to another Team Master that you are a member. The Team switcher drop-down will appear in the top right of your screen once you have been added as a member to another Team Master. The submitter needs to switch to the Team where they are a Team Guest member by selecting that team from the Team switcher drop-down. - As the submitter navigate to the helloworld-nodejs job on your new team and approve the
input
. Note the output of theecho
step.
NOTE: If you select a Pipeline job as a favorite you will be able to see things like jobs awaiting
input
submission in the Blue Ocean Dashboard.
Before moving on to the next lesson make sure that your Jenkinsfile Pipeline script is correct by comparing to or copying from below.
pipeline {
agent none
options {
buildDiscarder(logRotator(numToKeepStr: '2'))
skipDefaultCheckout true
}
stages {
stage('Test') {
agent {
kubernetes {
label 'nodejs-app-pod-2'
yamlFile 'nodejs-pod.yaml'
}
}
steps {
checkout scm
container('nodejs') {
echo 'Hello World!'
sh 'node --version'
}
}
}
stage('Build and Push Image') {
when {
beforeAgent true
beforeInput true
branch 'master'
}
steps {
echo "TODO - build and push image"
}
}
stage('Deploy') {
when {
beforeAgent true
branch 'master'
}
options {
timeout(time: 60, unit: 'SECONDS')
}
input {
message "Should we deploy?"
submitter "beedemo-ops"
submitterParameter "APPROVER"
}
steps {
echo "Continuing with deployment - approved by ${APPROVER}"
}
}
}
}
You've reached the end of the CloudBees Core workshop! Click here to head back to the main list of labs.
If you are interested to learn more about ways to monitor your Jenkins platform and measure to improve your DevOps performance using Value Streams, recommend proceeding to the CloudBees DevOptics Workshop.
You may also check out the other labs offered by us here: CloudBees Days Workshop.