Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

Latest commit

 

History

History
81 lines (73 loc) · 4.16 KB

conditional-when.md

File metadata and controls

81 lines (73 loc) · 4.16 KB

Conditional Execution using the when directive

In this exercise we will edit the Jenkinsfile file in your forked helloworld-nodejs repository with conditional execution using the when directive. We will accomplish this by adding a branch specific stage to the Jenkinsfile in your forked helloworld-nodejs repository.

  1. Navigate to and open the GitHub editor for the Jenkinsfile file in development branch of your forked helloworld-nodejs repository
  2. Insert the Build and Push Image stage after the existing Test stage and note the beforeAgent true option - this setting will result in the when condition being evaluated before acquiring and entering an agent for the stage. The branch condition is a built-in condition that allows executing stages only for specific branches - in this case the Build and Push Image stage will only execute for the master branch. The entire Pipeline shoud match what is below:
pipeline {
  agent none
  options { 
    buildDiscarder(logRotator(numToKeepStr: '2'))
    skipDefaultCheckout true
  }
  stages {
    stage('Test') {
      agent { label 'nodejs-app' }
      steps {
        checkout scm
        container('nodejs') {
          echo 'Hello World!'   
          sh 'node --version'
        }
      }
    }
    stage('Build and Push Image') {
      when {
         beforeAgent true
         branch 'master'
      }
      steps {
         echo "TODO - build and push image"
      }
    }
  }
}
  1. Commit the changes and then navigate to the helloworld-nodejs job in Blue Ocean on your Team Master and the job for the development branch should be running or queued to run. Note that the Build and Push Image stage was skipped.

  2. Now we will create a Pull Request between the development branch and master branch of your forked helloworld-nodejs repository. Navigate to your forked helloworld-nodejs repository in GitHub - click on the New pull request button

  3. Change the base repository to the master branch of your forked helloworld-nodejs repository (not the cloudbees-days repository), add a comment and then click the Create pull request button

  4. A job will be created for the pull request and once it has completed successfully your pull request will show that All checks have passed. Go ahead and click the Merge pull request button and then click the Confirm merge button but DO NOT delete the development branch

  5. 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. Click on the run and after it has completed notice that the Build and Push Image stage was not skipped

Next Lesson

Before moving on to the next lesson make sure that your Jenkinsfile Pipeline script on the master branch of your forked helloworld-nodejs repository matches the one from below:

Finished Jenkinsfile for Conditional Execution using the when directive lab

pipeline {
  agent none
  options { 
    buildDiscarder(logRotator(numToKeepStr: '2'))
    skipDefaultCheckout true
  }
  stages {
    stage('Test') {
      agent { label 'nodejs-app' }
      steps {
        checkout scm
        container('nodejs') {
          echo 'Hello World!'   
          sh 'node --version'
        }
      }
    }
    stage('Build and Push Image') {
      when {
        beforeAgent true
        branch 'master'
      }
      steps {
        echo "TODO - build and push image"
      }
    }
  }
}

You may proceed to the next lab Lab 4 - Custom Pipeline Pod Templates or head back to the main list of the labs when you are ready.