Jenkins – Check for the existence of a variable


If a variable is used in Jenkins Pipeline that is created on the basis of a Webhook or etc, then a task will fail with a manual start. To avoid this, you can add a check for the existence of a variable and set its value.

pipeline {
  agent any
    stage('Check ENV') {
      when { expression { env.GIT_COMMIT_ID == null } }
      steps {
        script {
          echo "COMMIT ID IS NOT SET"
          env.GIT_COMMIT_ID = sh(script: 'git log --format="%H" -n 1', returnStdout: true).trim()


In this example, the presence of the variable "GIT_COMMIT_ID" is checked, and if it is absent, a script is executed that sets this variable with the value of the last HASH commit for this branch. This check should be performed only after the "checkout" stage.

Tagged: Tags

Notify of

Inline Feedbacks
View all comments