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

GIT_BRANCH set to detached when sha1 parameter set in notifyCommit URL

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

      We recently upgraded our stash install including the stash jenkins webhook. Now this plugin has recently started calling the Jenkins git notifyCommit endpoint with branches and sha1 query string parameters set in the URL, e.g. it now calls URLs like

      /jenkins/git/notifyCommit?url=ssh%3A%2F%2Fgit%40my-stash-host%3A7999%2Fproject%2Frepository.git&branches=feature/XXXX-2573&sha1=b3dd6fe0a7db3b78739cf05c79a85bad56b3bc11

      This looks valid according to the Git plugin wiki.

      It used to NOT set the branches and sha1 parameter, e.g. simply ...

      /jenkins/git/notifyCommit?url=ssh%3A%2F%2Fgit%40my-stash-host%3A7999%2Fproject%2Frepository.git

      Since this webhook started calling this endpoint with the sha1 parameter our Jenkins jobs has started setting the GIT_BRANCH variable to detached and not the appropriate branch name.

      Without this sha1 parameter the job output starts with "Started by an SCM change". With this sha1 parameter the job output starts with e.g. "commit notification b3dd6fe0a7db3b78739cf05c79a85bad56b3bc11".

      e.g. Without SHA1 set we see

      Started by an SCM change
      Building on master in workspace /home/jenkins/jobs/My Job/workspace
      > /usr/local/bin/git rev-parse --is-inside-work-tree # timeout=10
      Fetching changes from the remote Git repository
      > /usr/local/bin/git config remote.origin.url ssh://git@my-stash-host:7999/project/repository.git # timeout=10
      Fetching upstream changes from ssh://git@my-stash-host:7999/project/repository.git
      > /usr/local/bin/git --version # timeout=10
      > /usr/local/bin/git fetch --tags --progress ssh://git@my-stash-host:7999/project/repository.git +refs/heads/*:refs/remotes/origin/*
      Seen branch in repository origin/HEAD
      Seen branch in repository origin/feature/XXXX-1018
      Seen branch in repository origin/feature/XXXX-1093
      ...
      
      Seen 293 remote branches
      Checking out Revision b3dd6fe0a7db3b78739cf05c79a85bad56b3bc11 (origin/feature/XXXX-2571)
      

      e.g. With SHA1 set

      commit notification b3dd6fe0a7db3b78739cf05c79a85bad56b3bc11
      Building on master in workspace /home/jenkins/jobs/My Job/workspace
      > /usr/local/bin/git rev-parse --is-inside-work-tree # timeout=10
      Fetching changes from the remote Git repository
      > /usr/local/bin/git config remote.origin.url ssh://git@my-stash-host:7999/project/repository.git # timeout=10
      Fetching upstream changes from ssh://git@my-stash-host:7999/project/repository.git
      > /usr/local/bin/git --version # timeout=10
      > /usr/local/bin/git fetch --tags --progress ssh://git@my-stash-host:7999/project/repository.git +refs/heads/*:refs/remotes/origin/*
      > /usr/local/bin/git rev-parse b3dd6fe0a7db3b78739cf05c79a85bad56b3bc11^{commit} # timeout=10
      Checking out Revision b3dd6fe0a7db3b78739cf05c79a85bad56b3bc11(detached)
      

      Note that Checking out Revision line knows the branch (AND sets GIT_BRANCH variable to the correct branch name) without SHA1 set but it does not know the branch, i.e. it sets detached for the branch name (AND sets GIT_BRANCH to detached).

      Note we have implemented a work around in apache by removing the sha1 parameter from the URL which fixes this for our environment (however we don't benefit from the notifyCommit being explicit about the commit revision to build).

      FYI - our Apache configuration workaround is:

      RewriteEngine On
      
      # Remove branches and sha1 from notify notifyCommit calls
      RewriteCond %{QUERY_STRING} ^url=([^&]*)&branches=([^&]*)&sha1=([^&]*)$ [NC]
      RewriteRule ^ %{REQUEST_URI}?url=%1&branches=%2 [PT]
      

          [JENKINS-24133] GIT_BRANCH set to detached when sha1 parameter set in notifyCommit URL

          Felipe Nascimento added a comment - - edited

          I don't know if its useful for you but, after looking at the output of the env command between build start and git checkout, I found that the GIT_BRANCH environment variable was set with the branch name like origin/featurebranchname, as that was enough for me I set the build name to ${ENV,var="GIT_BRANCH"} and it worked.

          Actually the way i thought was working wasn't. So i used the pre-scm-buildstep plugin to create a properties file using the following shell command:

          git reflog show --all | grep `echo $GIT_COMMIT | cut  -c1-7` | sed "s/.*\/\(.*\)@{.*/git.branch=\1/g" > jenkins_build.properties
          

          Basically it set the property git.branch on the file jenkins_build.properties to the branch name found using the first 7 chars from commit id.

          At the build-name-setter plugin property I used this macro:

          ${PROPFILE,file="jenkins_build.properties",property="git.branch"}
          

          Felipe Nascimento added a comment - - edited I don't know if its useful for you but, after looking at the output of the env command between build start and git checkout, I found that the GIT_BRANCH environment variable was set with the branch name like origin/featurebranchname , as that was enough for me I set the build name to ${ENV,var="GIT_BRANCH"} and it worked. Actually the way i thought was working wasn't. So i used the pre-scm-buildstep plugin to create a properties file using the following shell command: git reflog show --all | grep `echo $GIT_COMMIT | cut -c1-7` | sed "s/.*\/\(.*\)@{.*/git.branch=\1/g" > jenkins_build.properties Basically it set the property git.branch on the file jenkins_build.properties to the branch name found using the first 7 chars from commit id. At the build-name-setter plugin property I used this macro: ${PROPFILE,file="jenkins_build.properties",property="git.branch"}

          I noticed that the $GIT_COMMIT variable isn't updated before the scm, only after checkout.

          So we removed the pre-scm-buildstep and added the Environment Script Plugin which is executed between scm and build-name-setter plugin.

          With the Environment Script Plugin we used the same shell to create the jenkins_build.properties file.

          Felipe Nascimento added a comment - I noticed that the $GIT_COMMIT variable isn't updated before the scm, only after checkout. So we removed the pre-scm-buildstep and added the Environment Script Plugin which is executed between scm and build-name-setter plugin . With the Environment Script Plugin we used the same shell to create the jenkins_build.properties file.

          Adam Guja added a comment -

          still present in 2.6.0, on "Trigger build" from Stash
          checking "Omit SHA1 Hash Code" solves the #detached issue, but disables the "Trigger build" button

          Adam Guja added a comment - still present in 2.6.0, on "Trigger build" from Stash checking "Omit SHA1 Hash Code" solves the #detached issue, but disables the "Trigger build" button

          Jenkins ver. 1.580.1
          Git Plugin 2.2.7
          Bitbucket Plugin 1.0
          The hook was JENKINS_URL/bitbucket-hook/

          I noticed that GIT_BRANCH was broken (detached) even in builds (jobs) not configured with "Build when a change is pushed to BitBucket" but just SCM polling.

          valentino miazzo added a comment - Jenkins ver. 1.580.1 Git Plugin 2.2.7 Bitbucket Plugin 1.0 The hook was JENKINS_URL/bitbucket-hook/ I noticed that GIT_BRANCH was broken (detached) even in builds (jobs) not configured with "Build when a change is pushed to BitBucket" but just SCM polling.

          Rowan Beentje added a comment -

          Also saw this when updating to the Stash jenkins webhook 2.6.0. Supporting calling notifyCommit with the branch and SHA would be incredibly useful - it enables a "Trigger build" button in the Stash UI, for example, which triggers a build of a specific revision for testing. However this currently causes the detached GIT_BRANCH issue above, so we've had to roll the webhook back again...

          Rowan Beentje added a comment - Also saw this when updating to the Stash jenkins webhook 2.6.0. Supporting calling notifyCommit with the branch and SHA would be incredibly useful - it enables a "Trigger build" button in the Stash UI, for example, which triggers a build of a specific revision for testing. However this currently causes the detached GIT_BRANCH issue above, so we've had to roll the webhook back again...

          Mark Waite added a comment -

          Proposed fix has been merged for inclusion in the first versions after git plugin 2.3 and git plugin 2.2.8.

          Mark Waite added a comment - Proposed fix has been merged for inclusion in the first versions after git plugin 2.3 and git plugin 2.2.8.

          Code changed in jenkins
          User: Pavel Baranchikov
          Path:
          src/test/java/hudson/plugins/git/GitSCMTest.java
          http://jenkins-ci.org/commit/git-plugin/d7e78e24f0228cee9d588cb010bd6485fdfbcc38
          Log:
          Fixed JUnit tests for JENKINS-24133 on Windows

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Pavel Baranchikov Path: src/test/java/hudson/plugins/git/GitSCMTest.java http://jenkins-ci.org/commit/git-plugin/d7e78e24f0228cee9d588cb010bd6485fdfbcc38 Log: Fixed JUnit tests for JENKINS-24133 on Windows

          Code changed in jenkins
          User: Mark Waite
          Path:
          src/test/java/hudson/plugins/git/GitSCMTest.java
          http://jenkins-ci.org/commit/git-plugin/a1d3f789cf2ad6e430d596081c03d5ff4a34be50
          Log:
          Merge pull request #274 from pbaranchikov/detachedSha1

          Fixed JUnit tests for JENKINS-24133 on Windows

          Compare: https://github.com/jenkinsci/git-plugin/compare/6ab7413b439e...a1d3f789cf2a

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Mark Waite Path: src/test/java/hudson/plugins/git/GitSCMTest.java http://jenkins-ci.org/commit/git-plugin/a1d3f789cf2ad6e430d596081c03d5ff4a34be50 Log: Merge pull request #274 from pbaranchikov/detachedSha1 Fixed JUnit tests for JENKINS-24133 on Windows Compare: https://github.com/jenkinsci/git-plugin/compare/6ab7413b439e...a1d3f789cf2a

          Code changed in jenkins
          User: Pavel Baranchikov
          Path:
          src/test/java/hudson/plugins/git/GitSCMTest.java
          http://jenkins-ci.org/commit/git-plugin/fe438512a07059ecd954e1271d74c6ba24b536e0
          Log:
          Fixed JUnit tests for JENKINS-24133 on Windows

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Pavel Baranchikov Path: src/test/java/hudson/plugins/git/GitSCMTest.java http://jenkins-ci.org/commit/git-plugin/fe438512a07059ecd954e1271d74c6ba24b536e0 Log: Fixed JUnit tests for JENKINS-24133 on Windows

          Mark Waite added a comment -

          Fix included in git plugin 2.3.1, released 29 Nov 2014

          Mark Waite added a comment - Fix included in git plugin 2.3.1, released 29 Nov 2014

            ndeloof Nicolas De Loof
            ihomer Ian Homer
            Votes:
            16 Vote for this issue
            Watchers:
            22 Start watching this issue

              Created:
              Updated:
              Resolved: