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.

          feng yu created issue -
          feng yu made changes -
          Description Original: Here is my pipeline code:

          {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('|')
          {code}

          But finally it gives me this error:

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

          Is the environment variables still working for pipeline?
          New: Here is my pipeline code:

          {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('|')
          }

          {code}

          But finally it gives me this error:

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

          Is the environment variables still working for pipeline?

          Mark Waite added a comment -

          abcfy2 I think this might be an unintended side effect of the changes in SECURITY-170, but I'm not absolutely sure of that. I don't have time right now to duplicate your pipeline job setup.

          Please try the same setup in a Jenkins server that was started with the property

          hudson.model.ParametersAction.keepUndefinedParameters=true
          

          It may also be that there is some mistake in your referencing GIT_BRANCH without calling it env.GIT_BRANCH.

          Mark Waite added a comment - abcfy2 I think this might be an unintended side effect of the changes in SECURITY-170 , but I'm not absolutely sure of that. I don't have time right now to duplicate your pipeline job setup. Please try the same setup in a Jenkins server that was started with the property hudson.model.ParametersAction.keepUndefinedParameters=true It may also be that there is some mistake in your referencing GIT_BRANCH without calling it env.GIT_BRANCH.

          feng yu added a comment - - edited

          Thank you. I've tried your solutions, but all of them still not working.

          I can use the "GIT_*" variables in the free style jobs. such as :

          branch to be published: ${GIT_BRANCH}
          
          rev: ${GIT_COMMIT}
          
          commit user: ${GIT_AUTHOR_NAME}<${GIT_AUTHOR_EMAIL}>
          
          changelog:
          ${CHANGES, showPaths=true} 
          

          But all the variables above cannot be used in pipeline script.

          feng yu added a comment - - edited Thank you. I've tried your solutions, but all of them still not working. I can use the "GIT_*" variables in the free style jobs. such as : branch to be published: ${GIT_BRANCH} rev: ${GIT_COMMIT} commit user: ${GIT_AUTHOR_NAME}<${GIT_AUTHOR_EMAIL}> changelog: ${CHANGES, showPaths= true } But all the variables above cannot be used in pipeline script.

          Mark Waite added a comment -

          There is an article on stackoverflow.com which seems to indicate this is a known limitation of either pipeline or the git plugin. Sorry, but I'm not sure which is expected to provide that.

          Mark Waite added a comment - There is an article on stackoverflow.com which seems to indicate this is a known limitation of either pipeline or the git plugin. Sorry, but I'm not sure which is expected to provide that.

          feng yu added a comment -

          Great. It's a good way to solve this issue.

          feng yu added a comment - Great. It's a good way to solve this issue.
          Mark Waite made changes -
          Assignee Original: Mark Waite [ markewaite ]
          Mark Waite made changes -
          Link New: This issue is duplicated by JENKINS-36436 [ JENKINS-36436 ]

          Deleted User added a comment - - edited

          This is affecting me as well. Does the git plugin need to be subtly adjusted to export these critical pieces of data in a new way to be accessible to serial pipeline steps?

          In a custom plugin triggered after the Git step, the following are the only EnvVars available:

          BUILD_DISPLAY_NAME=#9
          BUILD_ID=9
          BUILD_NUMBER=9
          BUILD_TAG=jenkins-pipetest-9
          BUILD_TIMESTAMP=2016-07-22 11:30:39 CDT
          BUILD_URL=http://blah:8080/job/pipetest/9/
          CLASSPATH=
          HUDSON_HOME=/var/jenkins_home
          HUDSON_SERVER_COOKIE=blah
          HUDSON_URL=http://blah:8080/
          JENKINS_HOME=/var/jenkins_home
          JENKINS_SERVER_COOKIE=blah
          JENKINS_URL=http://blah:8080/
          JOB_BASE_NAME=pipetest
          JOB_NAME=pipetest
          JOB_URL=http://blah:8080/job/pipetest/
          

          Since BUILD_TIMESTAMP is available, that implies the https://wiki.jenkins-ci.org/display/JENKINS/Build+Timestamp+Plugin plugin is able to inject env vars into the pipeline just fine, so it is possible.

          Deleted User added a comment - - edited This is affecting me as well. Does the git plugin need to be subtly adjusted to export these critical pieces of data in a new way to be accessible to serial pipeline steps? In a custom plugin triggered after the Git step, the following are the only EnvVars available: BUILD_DISPLAY_NAME=#9 BUILD_ID=9 BUILD_NUMBER=9 BUILD_TAG=jenkins-pipetest-9 BUILD_TIMESTAMP=2016-07-22 11:30:39 CDT BUILD_URL=http://blah:8080/job/pipetest/9/ CLASSPATH= HUDSON_HOME=/var/jenkins_home HUDSON_SERVER_COOKIE=blah HUDSON_URL=http://blah:8080/ JENKINS_HOME=/var/jenkins_home JENKINS_SERVER_COOKIE=blah JENKINS_URL=http://blah:8080/ JOB_BASE_NAME=pipetest JOB_NAME=pipetest JOB_URL=http://blah:8080/job/pipetest/ Since BUILD_TIMESTAMP is available, that implies the https://wiki.jenkins-ci.org/display/JENKINS/Build+Timestamp+Plugin plugin is able to inject env vars into the pipeline just fine, so it is possible.

          Mark Waite added a comment -

          rboyer nice find that the build timestamp plugin has the necessary technique in it. Are you able to review the source code of the build timestamper plugin and submit a comparable pull request for the git plugin?

          Mark Waite added a comment - rboyer nice find that the build timestamp plugin has the necessary technique in it. Are you able to review the source code of the build timestamper plugin and submit a comparable pull request for the git plugin?

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

              Created:
              Updated:
              Resolved: