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

The environment variables of git plugin not working in pipeline script.

      Here is my pipeline code:

      node {
          checkout changelog: true,
              poll: true,
              scm: [
                  $class: 'GitSCM', 
                  branches: [[name: "${env.gitlabSourceRepoName}/${env.gitlabSourceBranch}"]],
                  doGenerateSubmoduleConfigurations: false, 
                  extensions: [
                      [$class: 'PruneStaleBranch'],
                      [
                          $class: 'PreBuildMerge', options: [
                              fastForwardMode: 'FF', 
                              mergeRemote: 'origin', 
                              mergeStrategy: 'default', 
                              mergeTarget: "${env.gitlabTargetBranch}"
                          ]
                      ],
                      [$class: 'CleanCheckout']
                  ], 
                  userRemoteConfigs: [
                      [name: 'origin', url: 'git@gitlab.mydomain.com:user/project.git', credentialsId: '8949144f-cca9-4385-8597-ad3c14bbd7ce'],
                      [name: "${env.gitlabSourceRepoName}", url: "${env.gitlabSourceRepoURL}", credentialsId: '8949144f-cca9-4385-8597-ad3c14bbd7ce']
                  ],
                  browser: [$class: 'GitLab', repoUrl: 'http://gitlab.mydomain.com', version: '8.8']
              ]
      
          echo """
                   |GIT_COMMIT: ${GIT_COMMIT}
                   |GIT_BRANCH: ${GIT_BRANCH }
                   """.stripMargin('|')
      }
      
      

      But finally it gives me this error:

      groovy.lang.MissingPropertyException: No such property: GIT_BRANCH for class: WorkflowScript
      

      Is the environment variables still working for pipeline?

          [JENKINS-35230] The environment variables of git plugin not working in pipeline script.

          Jesse Glick added a comment -

           a bug in the documentation?

          A bug in which documentation? URLs please.

           is the lack of env var exposing a bug, technical limitation or on purpose (as-designed)?

          As designed. checkout is a non-block-scoped step. Environment variables are only set by block-scoped steps. If you wish to pass things to external processes, use withEnv.

          Jesse Glick added a comment -  a bug in the documentation? A bug in which documentation? URLs please.  is the lack of env var exposing a bug, technical limitation or on purpose (as-designed)? As designed. checkout is a non-block-scoped step. Environment variables are only set by block-scoped steps. If you wish to pass things to external processes, use withEnv .

          Patrick Rose added a comment -

          Patrick Rose added a comment - A bug in which documentation? URLs please https://wiki.jenkins.io/display/JENKINS/Git+Plugin#GitPlugin-Environmentvariables

          Jesse Glick added a comment -

          Well, that was written a long time ago of course, for freestyle projects. I have PRs in flight affecting the live documentation inside the product, which is generally more accurate than wikis.

          Jesse Glick added a comment - Well, that was written a long time ago of course, for freestyle projects. I have PRs in flight affecting the live documentation inside the product, which is generally more accurate than wikis.

          Is this Resolved? 

          If there's a work around to get the environment variables for Git? Please point me to that.

          For now, I'm getting the env list for Free-Style projects from the below,

          <snip>

          EnvVars env = run.getEnvironment(taskListener);

          </snip>

          Hariharan Kuppusamy added a comment - Is this Resolved?  If there's a work around to get the environment variables for Git? Please point me to that. For now, I'm getting the env list for Free-Style projects from the below, <snip> EnvVars env = run.getEnvironment(taskListener); </snip>

          Mark Waite added a comment -

          hariharan yes, it is resolved. The values are returned by the pipeline checkout command as a map.

          Refer to a sample Jenkinsfile that uses the returned map to perform some tests on the values of those variables.

          Mark Waite added a comment - hariharan yes, it is resolved. The values are returned by the pipeline checkout command as a map. Refer to a sample Jenkinsfile that uses the returned map to perform some tests on the values of those variables.

          Thanks Mark,

          As suggested, I can able to get the Git details with Pipeline SCM plugin 2.6,

          <snip>

          map_vars = checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'admin', url: 'http://test.git.url']])|http://test.git.url%27]]]%29/]

          print map_vars

          </snip>

          Is there a way to send/inject these details into Jenkins AbstractBuild/Run or TaskListener object? Actually I need to use these details in my plugin.

           

           

          Hariharan Kuppusamy added a comment - Thanks Mark, As suggested, I can able to get the Git details with Pipeline SCM plugin 2.6, <snip> map_vars = checkout([$class: 'GitSCM', branches: [ [name: '*/master'] ], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'admin', url: ' http://test.git.url' ]])|http://test.git.url%27]]]%29/] print map_vars </snip> Is there a way to send/inject these details into Jenkins AbstractBuild/Run or TaskListener object? Actually I need to use these details in my plugin.    

          Mark Waite added a comment - - edited

          hariharan, I don't know the technique you would use to obtain those values in your plugin by calling the git plugin. Possibly @jglick could offer a suggestion?

          Mark Waite added a comment - - edited hariharan , I don't know the technique you would use to obtain those values in your plugin by calling the git plugin. Possibly @jglick could offer a suggestion?

          Jesse Glick added a comment -

          The values will not be accessible from another plugin, by design. Your plugin may accept standard data-bound parameters, and a user’s script may pass variables from checkout (in original or modified form) to your plugin, or not.

          Jesse Glick added a comment - The values will not be accessible from another plugin, by design. Your plugin may accept standard data-bound parameters, and a user’s script may pass variables from checkout (in original or modified form) to your plugin, or not.

          Jared Miller added a comment -

          This is not at all obvious if you just glance over: https://wiki.jenkins.io/display/JENKINS/Git+Plugin

          You'd never expect that to access those ENV variables you have to capture the output of checkout.

          Or am I mistaken? 

          Jared Miller added a comment - This is not at all obvious if you just glance over: https://wiki.jenkins.io/display/JENKINS/Git+Plugin You'd never expect that to access those ENV variables you have to capture the output of checkout. Or am I mistaken? 

          Mark Waite added a comment -

          shmup, the wiki is not the preferred location for Pipeline documentation. Refer to the Pipeline: SCM Step page for an example using the environment variables as a map returned from the `checkout` step. There is a Jenkins Minute video that describes it as well. You can also read about it (briefly) in the Pipeline: SCM Step plugin page.

          If you'd like to add it to the Git plugin Wiki, you're welcome to do that as well.

          Mark Waite added a comment - shmup , the wiki is not the preferred location for Pipeline documentation. Refer to the Pipeline: SCM Step page for an example using the environment variables as a map returned from the `checkout` step. There is a Jenkins Minute video that describes it as well. You can also read about it (briefly) in the Pipeline: SCM Step plugin page . If you'd like to add it to the Git plugin Wiki, you're welcome to do that as well.

            Unassigned Unassigned
            abcfy2 feng yu
            Votes:
            29 Vote for this issue
            Watchers:
            45 Start watching this issue

              Created:
              Updated:
              Resolved: