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

env.GIT_COMMIT is overwritten when pull lib (lib and code in the same repo), can not get the right GIT_COMMIT

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • git-plugin
    • None

      env.GIT_COMMIT is overwritten when pull lib (lib and code in the same repo)

       

      SharedLibrary put in the same code repo, different branch named xxx_lib, and after Declarative: Checkout SCM, can not get the right GIT_COMMIT, always be the xxx_lib's last commit, not the code's commit

       

       

      I create example repo to this issue: https://github.com/bonfy/JENKINS-61317

      Jenkinsfile in master branch Last commit: 39f25e1a
      library in jenkins-lib branch Last commit: a8c235b1

      and when echo GIT_COMMIT, it is overwitten by lib's commit

      16:55:47 stage 1
      [Pipeline] echo
      16:55:47 GIT COMMIT : a8c235b10c55e2f065293b1c1ad9b3bffd45d9d9
      [Pipeline] echo
      16:55:47 GIT BRANCH : jenkins-lib
      [Pipeline] echo
      16:55:48 Hello world

       

          [JENKINS-61317] env.GIT_COMMIT is overwritten when pull lib (lib and code in the same repo), can not get the right GIT_COMMIT

          Mark Waite added a comment -

          Please provide an example pipeline which shows the issue using a publicly accessible git repository.

          Mark Waite added a comment - Please provide an example pipeline which shows the issue using a publicly accessible git repository.

          bon ch added a comment -

          markewaite

          I create example repo to this issue: https://github.com/bonfy/JENKINS-61317

          Jenkinsfile in master branch Last commit : 64dd9920
          library in jenkins-lib branch Last commit: a8c235b1

          and when echo GIT_COMMIT, it is overwitten by lib's commit

          [Pipeline] echo
          13:22:18 stage 1
          [Pipeline] echo
          13:22:18 GIT COMMIT : a8c235b10c55e2f065293b1c1ad9b3bffd45d9d9
          [Pipeline] echo
          13:22:18 Hello world
          [Pipeline] }

           

          bon ch added a comment - markewaite I create example repo to this issue: https://github.com/bonfy/JENKINS-61317 Jenkinsfile in master branch Last commit : 64dd9920 library in jenkins-lib branch Last commit: a8c235b1 and when echo GIT_COMMIT, it is overwitten by lib's commit [Pipeline] echo 13:22:18 stage 1 [Pipeline] echo 13:22:18 GIT COMMIT : a8c235b10c55e2f065293b1c1ad9b3bffd45d9d9 [Pipeline] echo 13:22:18 Hello world [Pipeline] }  

          Mark Waite added a comment - - edited

          Thank you. Until a fix is available for the issue, you can use the returnStdout capability of the sh and bat and powershell commands to call git rev-parse HEAD

          Mark Waite added a comment - - edited Thank you. Until a fix is available for the issue, you can use the returnStdout capability of the sh and bat and powershell commands to call git rev-parse HEAD

          bon ch added a comment -

          ok, thx

          bon ch added a comment - ok, thx

          bon ch added a comment -

          markewaite

           

          I update the repo https://github.com/bonfy/JENKINS-61317

          Add echo env.GIT_BRANCH, and find that the GIT_BRANCH is also overwritten, maybe other git related env valuables as well.  So maybe you can check and fix them together.

           

          the Jenkins console output now looks like: 

           

          16:55:47 stage 1
          [Pipeline] echo
          16:55:47 GIT COMMIT : a8c235b10c55e2f065293b1c1ad9b3bffd45d9d9
          [Pipeline] echo
          16:55:47 GIT BRANCH : jenkins-lib
          [Pipeline] echo
          16:55:48 Hello world

           

          Thanks

          bon ch added a comment - markewaite   I update the repo  https://github.com/bonfy/JENKINS-61317 Add echo env.GIT_BRANCH, and find that the GIT_BRANCH is also overwritten, maybe other git related env valuables as well.  So maybe you can check and fix them together.   the Jenkins console output now looks like:    16:55:47 stage 1 [Pipeline] echo 16:55:47 GIT COMMIT : a8c235b10c55e2f065293b1c1ad9b3bffd45d9d9 [Pipeline] echo 16:55:47 GIT BRANCH : jenkins-lib [Pipeline] echo 16:55:48 Hello world   Thanks

          Dee Kryvenko added a comment -

          Dee Kryvenko added a comment - Seems like the same as  https://issues.jenkins-ci.org/browse/JENKINS-45489

          JC added a comment -

          Additionally to all the info already provided, when importing the libraries from git, this happen only with http checkout. SSH works correctly and don't generate the issue. E.G:

          library(
                identifier: 'shared-library@master',
                retriever: modernSCM(
                  [
                    $class: 'GitSCMSource',
                    remote: "git@github.com:TEST/shared-library.git",
                    credentialsId: 'SSH',
                    extensions: [[$class: 'WipeWorkspace']]
                  ]
                )
              )
          

           

          JC added a comment - Additionally to all the info already provided, when importing the libraries from git, this happen only with http checkout. SSH works correctly and don't generate the issue. E.G: library( identifier: 'shared-library@master' , retriever: modernSCM( [ $class: 'GitSCMSource' , remote: "git@github.com:TEST/shared-library.git" , credentialsId: 'SSH' , extensions: [[$class: 'WipeWorkspace' ]] ] ) )  

          Mark Waite added a comment - - edited

          juanchocol69 When I checkout the library and the Jenkinsfile from ssh, it shows the problem. When I use https, it does not show the problem.

          The problem seems to be specific to individual Pipeline jobs. Pipeline jobs created by a multibranch Pipeline or an organization folder can read the return value of checkout scm to get the correct values for GIT_COMMIT and GIT_BRANCH since the fix of JENKINS-53346 in git plugin 4.7.0. The fix of JENKINS-53346 only applies to multibranch Pipeline and organization folder Pipeline, not to individual Pipeline jobs.

          The same workaround will need to continue to be used. Command line calls to git to report the state of the workspace.

          Refer to my multibranch test case for the checkout scm technique.

          Refer to my ssh example for a case that shows the failure.

          Refer to my https example for another case that shows the failure.

          Mark Waite added a comment - - edited juanchocol69 When I checkout the library and the Jenkinsfile from ssh, it shows the problem. When I use https, it does not show the problem. The problem seems to be specific to individual Pipeline jobs. Pipeline jobs created by a multibranch Pipeline or an organization folder can read the return value of checkout scm to get the correct values for GIT_COMMIT and GIT_BRANCH since the fix of JENKINS-53346 in git plugin 4.7.0. The fix of JENKINS-53346 only applies to multibranch Pipeline and organization folder Pipeline, not to individual Pipeline jobs. The same workaround will need to continue to be used. Command line calls to git to report the state of the workspace. Refer to my multibranch test case for the checkout scm technique. Refer to my ssh example for a case that shows the failure. Refer to my https example for another case that shows the failure.

          James Nord added a comment -

          I believe this can also happen with the following pipeline

          node() {
              dir('first') {
                  def vars = checkout scm
                  dumpEnv('first', vars)
              }
              dir('second') {
                  def vars = git credentialsId: '<same-as-scm>', url: '<same-as-scm>', branch: 'main'
                  dumpEnv('second', vars)
              }
              dir('third') {
                  def vars = checkout scm
                  dumpEnv('third', vars)
              }
          }
          
          
          def dumpEnv(String prefix, Map env) {
              env.each { key, value -> echo("${prefix}: '${key}' = '${value}'") }
          }
          
          
          

          The first and third dumpEnv calls should output the same content

          James Nord added a comment - I believe this can also happen with the following pipeline node() { dir( 'first' ) { def vars = checkout scm dumpEnv( 'first' , vars) } dir( 'second' ) { def vars = git credentialsId: '<same-as-scm>' , url: '<same-as-scm>' , branch: 'main' dumpEnv( 'second' , vars) } dir( 'third' ) { def vars = checkout scm dumpEnv( 'third' , vars) } } def dumpEnv( String prefix, Map env) { env.each { key, value -> echo( "${prefix}: '${key}' = '${value}' " ) } } The first and third dumpEnv calls should output the same content

            Unassigned Unassigned
            bonfy bon ch
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: