Skip to content

Latest commit

 

History

History
158 lines (132 loc) · 9.19 KB

input.md

File metadata and controls

158 lines (132 loc) · 9.19 KB

Pipelines with Interactive Input

In this exercise, we will see how you can capture interactive input in your Jenkins Pipeline while it is running.

  1. 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 Image stage 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.

  1. 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 the Deploy stage. Go ahead and click the Proceed button and the job will complete successfully. The input prompt is also available in the Console log and classic Stage View.

  2. 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 used options at the global pipeline level to set the Discard old builds strategy for your Team Master with the buildDiscarder option. Now we will configure options at the stage level. We will add a timeout option for the Deploy stage using the stage options directive. Update the Deploy stage 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"
      }
    }
  1. 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.

Input Approval for Team Members

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:

  1. 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).

  2. Next, click on the cog icon for your team.

  3. Click on the Members link in the left menu and then click on the Add a user or group link.

  4. 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.

  5. 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.

  1. 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 Deploy stage with the following changes (replacing beedemo-ops with Jenkins username of your new Team Guest member). Also, update the timeout duration to give your approver plenty of time to submit the input:
      options {
        timeout(time: 60, unit: 'SECONDS') 
      }
      input {
        message "Should we deploy?"
        submitter "beedemo-ops"
        submitterParameter "APPROVER"
      }
  1. So, we added one additonal configuration option for our input directive: submitterParameter. Setting the submitterParameter option will result in a Pipeline environmental variable named APPROVER being set with the value being the username of the user that submitted the input. In the example above it will be beedemo-ops. Update the steps section so the echo step in your Jenkinsfile Pipeline script will print the APPROVER environmental variable and then commit the changes:
      steps {
        echo "Continuing with deployment - approved by ${APPROVER}"
      }
  1. Navigate to the master branch of your helloworld-nodejs job in Blue Ocean on your Team Master. The job should be waiting for input:

  2. 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.

  3. As the submitter navigate to the helloworld-nodejs job on your new team and approve the input. Note the output of the echo 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.

Finished Jenkinsfile for Pipeline Approvals with Interactive Input

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.