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

Revision state returned by Git SCM step is incorrect

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Minor Minor
    • git-plugin, scm-api-plugin
    • None
    • Platform: Windows
      Jenkins Version: 2.121.1
      Git Plugin: 3.9.1
      Git Client: 2.7.2
      SCM API Plugin: 2.2.7
    • git plugin 4.7.0 released 17 Mar 2021

      When using a pipeline script from SCM which checks out a different revision, the checkout step returns the GIT_COMMIT of the original checkout.

      I see this manifested in my multibranch pipeline jobs, but it is best demonstrated by a simple parameterised pipeline. The job is configured with "refs/heads/master" as the branch specifier and no branching in the repository is required to reproduce the problem.

      The job has a single "TAG" parameter, providing the tag to be checked out by the pipeline:

      pipeline
      {
          agent any
          environment
          {
              git_tool = tool(name: 'Default', type: 'git')
          }
          options
          {
              skipDefaultCheckout()
          }
          stages
          {
              stage('Checkout')
              {
                  steps
                  {
                      script
                      {
                          def buildTag = env.TAG
                          echo "Building against tag '${buildTag}'"
      
                          def newScm = [$class: 'GitSCM', userRemoteConfigs: scm.userRemoteConfigs, branches: [[name: "refs/tags/${buildTag}"]]]
      
                          def checkoutDetails = checkout scm: newScm, poll: false, changelog: false
      
                          echo "checkout scm returned SHA = ${checkoutDetails.GIT_COMMIT}"
                          bat """git status"""
                      }
                  }
              }
          }
      }
      

      When run with a tag at the HEAD of the specified branch (master) we see the returned SHA is as expected:

      Building against tag 'tag3'
      [Pipeline] checkout
       > C:\Program Files\Git\cmd\git.exe rev-parse --is-inside-work-tree # timeout=120
      Fetching changes from the remote Git repository
       > C:\Program Files\Git\cmd\git.exe config remote.origin.url git@github.aus.thenational.com:P640806/jenkins_testing.git # timeout=120
      Fetching upstream changes from git@github.aus.thenational.com:P640806/jenkins_testing.git
       > C:\Program Files\Git\cmd\git.exe --version # timeout=120
      using GIT_SSH to set credentials 
       > C:\Program Files\Git\cmd\git.exe fetch --tags --progress git@github.aus.thenational.com:P640806/jenkins_testing.git +refs/heads/*:refs/remotes/origin/*
       > C:\Program Files\Git\cmd\git.exe rev-parse "refs/tags/tag3^{commit}" # timeout=120
       > C:\Program Files\Git\cmd\git.exe rev-parse "refs/remotes/origin/refs/tags/tag3^{commit}" # timeout=120
      Checking out Revision e03409cea735e7353e421551c6bd5963f436c38a (refs/tags/tag3)
       > C:\Program Files\Git\cmd\git.exe config core.sparsecheckout # timeout=120
       > C:\Program Files\Git\cmd\git.exe checkout -f e03409cea735e7353e421551c6bd5963f436c38a
      Commit message: "Removed env.GIT_COMMIT usage"
      [Pipeline] echo
      checkout scm returned SHA = e03409cea735e7353e421551c6bd5963f436c38a
      [Pipeline] bat
      [test_scm_issue] Running batch script
      
      d:\jenkins\ws\test_scm_issue>git status 
      HEAD detached at e03409c
      nothing to commit, working tree clean
      

      If an additional commit is added to master (so the tag is no longer at the HEAD), we see the correct revision (e03409c) is checked out, however the revision state returned by the checkout step is the new HEAD of master (5e70b9e903582cd1055e60b819c6f2f09f01aee7):

      Building against tag 'tag3'
      [Pipeline] checkout
       > C:\Program Files\Git\cmd\git.exe rev-parse --is-inside-work-tree # timeout=120
      Fetching changes from the remote Git repository
       > C:\Program Files\Git\cmd\git.exe config remote.origin.url git@github.aus.thenational.com:P640806/jenkins_testing.git # timeout=120
      Fetching upstream changes from git@github.aus.thenational.com:P640806/jenkins_testing.git
       > C:\Program Files\Git\cmd\git.exe --version # timeout=120
      using GIT_SSH to set credentials 
       > C:\Program Files\Git\cmd\git.exe fetch --tags --progress git@github.aus.thenational.com:P640806/jenkins_testing.git +refs/heads/*:refs/remotes/origin/*
       > C:\Program Files\Git\cmd\git.exe rev-parse "refs/tags/tag3^{commit}" # timeout=120
       > C:\Program Files\Git\cmd\git.exe rev-parse "refs/remotes/origin/refs/tags/tag3^{commit}" # timeout=120
      Checking out Revision e03409cea735e7353e421551c6bd5963f436c38a (refs/tags/tag3)
       > C:\Program Files\Git\cmd\git.exe config core.sparsecheckout # timeout=120
       > C:\Program Files\Git\cmd\git.exe checkout -f e03409cea735e7353e421551c6bd5963f436c38a
      Commit message: "Removed env.GIT_COMMIT usage"
      [Pipeline] echo
      checkout scm returned SHA = 5e70b9e903582cd1055e60b819c6f2f09f01aee7
      [Pipeline] bat
      [test_scm_issue] Running batch script
      
      d:\jenkins\ws\test_scm_issue>git status 
      HEAD detached at e03409c
      nothing to commit, working tree clean
      

      Maybe it's related to JENKINS-45489?

            Unassigned Unassigned
            kakapo4 Mark Wright
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: