Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-49174

Declarative Pipeline: GIT_* have the value 'null' when using "agent none"

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Major Major
    • None
    • CloudBees Jenkins 2.89.3-4-rolling (using Jenkins LTS 2.89.3), with recommended plugins installed (Declarative Pipeline 1.2.6)

      On a declarative pipeline, if you set a custom environment variable
      with some of the "GIT_" variables in the value, along with "agent none" directive , the the value "null" will be set to those "GIT_" .

      It make sense (thanks abayer for the explanation) since with no agent scheduled (so no executor), then no git initialization is done.

      The following pipeline illustrate this:

      pipeline {
        agent none
        environment {
          MY_VARIABLE="${GIT_COMMIT}"
        }
        stages {
          stage('Show Must Go On') {
            agent any
            steps {
              echo "${GIT_COMMIT}"
              echo "${MY_VARIABLE}"
            }
          }
        }
      }
      

      will output the following (461023f607c0bf73b3b6cdd4d5561b42b04b1937 is my commit ID):

      {code)
      ...
      [Pipeline] withEnv
      [Pipeline]

      { [Pipeline] echo 461023f607c0bf73b3b6cdd4d5561b42b04b1937 [Pipeline] echo null [Pipeline] }

      ...

      Problem solved by switching "agent none" to "agent any" on the top-level declaration, so the git repo is initialized, and so the "GIT_*" variables are as well, on the top declaration:
      

      pipeline {
      agent any
      environment {
      MY_VARIABLE="${GIT_COMMIT}"
      }
      stages {
      stage('Show Must Go On') {
      agent any
      steps {
      echo "${GIT_COMMIT}"
      echo "${MY_VARIABLE}"
      }
      }
      }
      }

      
      

      Should we might materialize this on the doc ?

          [JENKINS-49174] Declarative Pipeline: GIT_* have the value 'null' when using "agent none"

          Andrew Bayer added a comment -

          I'm honestly not sure where this would go in the docs?

          Andrew Bayer added a comment - I'm honestly not sure where this would go in the docs?

            Unassigned Unassigned
            dduportal Damien Duportal
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: