ghprb icon indicating copy to clipboard operation
ghprb copied to clipboard

Jenkins injects the second to last git commit as GIT_COMMIT env variable

Open kowalcj0 opened this issue 9 years ago • 11 comments

I don't know whether this is an issue with Jenkins, git-plugin or with ghprb so I've raised it first on Jenkins bug tracker: https://issues.jenkins-ci.org/browse/JENKINS-31686

I'm running: Jenkins v1.638 on Ubuntu 3.13.0-48-generic x86_64 with Oracle Java 1.8.0_66 and plugins:

  • GIT client plugin=1.19.0
  • GIT plugin=2.4.0
  • GitHub API Plugin=1.69
  • Github Authentication plugin=0.22.2
  • GitHub plugin=1.14.0
  • GitHub Pull Request Builder=1.29.4

Have a look at the excerpt from the build log. Compare GIT_COMMIT, GIT_PREVIOUS_COMMIT with ghprbActualCommit. It looks like git rev-list is using wrong commit_id

What's more surprising is that it's an intermittent issue. I've noticed that sometimes all is fine, an the next build will use wrong commit_id. Any ideas what might be wrong?

Thanks

GitHub pull request #21 of commit 91cdd0e62caf929448a09caf57df9f99bc7dafd6, no merge conflicts. GitHub pull request #21 of commit 91cdd0e62caf929448a09caf57df9f99bc7dafd6, no merge conflicts. Setting status of 91cdd0e62caf929448a09caf57df9f99bc7dafd6 to PENDING with url http://jenkins.net:8080/job/build-on-PR/82/ and message: 'Build started sha1 is merged.' [EnvInject] - Loading node environment variables. Building on master in workspace /var/lib/jenkins/workspace/build-on-PR [WS-CLEANUP] Deleting project workspace... [WS-CLEANUP] Done Cloning the remote Git repository Cloning repository [email protected]:example/com.git git init /var/lib/jenkins/workspace/build-on-PR/src/github.com/example/com # timeout=10 Fetching upstream changes from [email protected]:example/com.git git --version # timeout=10 using GIT_SSH to set credentials Used for Github integration git -c core.askpass=true fetch --tags --progress [email protected]:example/com.git +refs/heads/:refs/remotes/origin/ git config remote.origin.url [email protected]:example/com.git # timeout=10 git config --add remote.origin.fetch +refs/heads/:refs/remotes/origin/ # timeout=10 git config remote.origin.url [email protected]:example/com.git # timeout=10 Fetching upstream changes from [email protected]:example/com.git using GIT_SSH to set credentials Used for Github integration git -c core.askpass=true fetch --tags --progress [email protected]:example/com.git +refs/heads/:refs/remotes/origin/ +refs/pull/:refs/remotes/origin/pr/ git rev-parse 91cdd0e62caf929448a09caf57df9f99bc7dafd6^ {commit} timeout=10 Checking out Revision 91cdd0e62caf929448a09caf57df9f99bc7dafd6 (detached) git config core.sparsecheckout # timeout=10 git checkout -f 91cdd0e62caf929448a09caf57df9f99bc7dafd6 git rev-list 37b91ca3808f0fe9ae726c1562c1deddd5652ea0 # timeout=10 [build-on-PR] $ /bin/bash /tmp/hudson4235424762063686423.sh export PROJECTIMPORTPATH=github.com/example/com PROJECTIMPORTPATH=github.com/example/com sort env BUILD_CAUSE=GHPRBCAUSE,GHPRBCAUSE BUILD_CAUSE_GHPRBCAUSE=true BUILD_DISPLAY_NAME=#82 BUILD_ID=82 BUILD_NUMBER=82 BUILD_TAG=jenkins-build-on-PR-82 BUILD_URL=http://jenkins.net:8080/job/build-on-PR/82/ EXECUTOR_NUMBER=0 [email protected] GIT_AUTHOR_NAME=yoyojenkinsbot GIT_BRANCH=item-598 GIT_COMMIT=37b91ca3808f0fe9ae726c1562c1deddd5652ea0 [email protected] GIT_COMMITTER_NAME=yoyojenkinsbot GIT_PREVIOUS_COMMIT=37b91ca3808f0fe9ae726c1562c1deddd5652ea0 GIT_PREVIOUS_SUCCESSFUL_COMMIT=37b91ca3808f0fe9ae726c1562c1deddd5652ea0 [email protected]:example/com.git GOROOT=/usr/local/go/1.5.1 HOME=/var/lib/jenkins HUDSON_COOKIE=9bf6d035-21a3-480c-b345-3fc7245e160v HUDSON_HOME=/var/lib/jenkins HUDSON_SERVER_COOKIE=3fc7dd345e6d2e33 HUDSON_URL=http://jenkins.net:8080/ JENKINS_HOME=/var/lib/jenkins JENKINS_SCRIPTS=/var/lib/jenkins/workspace/jenkins-scripts/jenkins-scripts JENKINS_SERVER_COOKIE=3fc7dd720e6d2e56 JENKINS_URL=http://jenkins.net:8080/ JOB_NAME=build-on-PR JOB_URL=http://jenkins.net:8080/job/build-on-PR/ LANG=en_US.UTF-8 LC_COLLATE=C LOGNAME=jenkins MAIL=/var/mail/jenkins NLSPATH=/usr/dt/lib/nls/msg/%L/%N.cat NODE_LABELS=go master master, NODE_NAME=master PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/local/go/bin:/var/lib/jenkins/go/bin PDIR=src/github.com/example/com PROJECTIMPORTPATH=github.com/example/com PROJECTNAME=ms-psps PWD=/var/lib/jenkins/workspace/build-on-PR ROOT_BUILD_CAUSE=GHPRBCAUSE ROOT_BUILD_CAUSE_GHPRBCAUSE=true SHELL=/bin/bash SHLVL=2 TERM=xterm-256color USER=jenkins WORKSPACE=/var/lib/jenkins/workspace/build-on-PR XDG_RUNTIME_DIR=/run/user/1000 XDG_SESSION_ID=23 XFILESEARCHPATH=/usr/dt/app-defaults/%L/Dt _=/usr/bin/env ghprbActualCommit=91cdd0e62caf929448a09caf57df9f99bc7dafd6 ghprbActualCommitAuthor=Dev1 [email protected] ghprbAuthorRepoGitUrl=https://github.com/example/com.git ghprbCommentBody=test this please ghprbCredentialsId=6da1257c-1f41-4b30-ba9e-03af2f772ad1 ghprbGhRepository=example/com ghprbPullAuthorLogin=dev1 ghprbPullAuthorLoginMention=@dev1 ghprbPullDescription=GitHub pull request #21 of commit 91cdd0e62caf929448a09caf57df9f99bc7dafd6, no merge conflicts. ghprbPullId=21 ghprbPullLink=https://github.com/example/com/pull/21 ghprbPullLongDescription=null ghprbPullTitle=New backend feature ghprbSourceBranch=item-598 ghprbTargetBranch=master ghprbTriggerAuthor=Dev1 ghprbTriggerAuthorLogin=dev1 ghprbTriggerAuthorLoginMention=@dev1 sha1=origin/pr/21/merge

kowalcj0 avatar Nov 24 '15 16:11 kowalcj0

If you look at the sha1 you will notice that it is pointing to the github merged version of your branch and master. This creates a test commit, 37b91ca3808f0fe9ae726c1562c1deddd5652ea0, that is then checkout out to test against. This commit only exists for the successful merge.

DavidTanner avatar Nov 24 '15 16:11 DavidTanner

hmmmm....

so why in this case both commits are the same?

GitHub pull request #22 of commit bc1778116a147a6634191704922da33bd8cdc214, no merge conflicts. GitHub pull request #22 of commit bc1778116a147a6634191704922da33bd8cdc214, no merge conflicts. Setting status of bc1778116a147a6634191704922da33bd8cdc214 to PENDING with url http://jenkins.net:8080/job/build-on-PR/99/ and message: 'Build started sha1 is merged.' [EnvInject] - Loading node environment variables. Building on master in workspace /var/lib/jenkins/workspace/build-on-PR [WS-CLEANUP] Deleting project workspace... [WS-CLEANUP] Done Cloning the remote Git repository Cloning repository [email protected]:example/com.git git init /var/lib/jenkins/workspace/build-on-PR/src/github.com/example/com # timeout=10 Fetching upstream changes from [email protected]:example/com.git git --version # timeout=10 using GIT_SSH to set credentials Used for Github integration git -c core.askpass=true fetch --tags --progress [email protected]:example/com.git +refs/heads/:refs/remotes/origin/ git config remote.origin.url [email protected]:example/com.git # timeout=10 git config --add remote.origin.fetch +refs/heads/:refs/remotes/origin/ # timeout=10 git config remote.origin.url [email protected]:example/com.git # timeout=10 Fetching upstream changes from [email protected]:example/com.git using GIT_SSH to set credentials Used for Github integration git -c core.askpass=true fetch --tags --progress [email protected]:example/com.git +refs/heads/:refs/remotes/origin/ +refs/pull/:refs/remotes/origin/pr/ git rev-parse bc1778116a147a6634191704922da33bd8cdc214^{commit} # timeout=10 Checking out Revision bc1778116a147a6634191704922da33bd8cdc214 (detached) git config core.sparsecheckout # timeout=10 git checkout -f bc1778116a147a6634191704922da33bd8cdc214 git rev-list bc1778116a147a6634191704922da33bd8cdc214 # timeout=10 [build-on-PR] $ /bin/bash /tmp/hudson7499073686641826623.sh export PROJECTIMPORTPATH=github.com/example/com PROJECTIMPORTPATH=github.com/example/com env BUILD_CAUSE=GHPRBCAUSE,GHPRBCAUSE BUILD_CAUSE_GHPRBCAUSE=true BUILD_DISPLAY_NAME=#99 BUILD_ID=99 BUILD_NUMBER=99 BUILD_TAG=jenkins-build-on-PR-99 BUILD_URL=http://jenkins.net:8080/job/build-on-PR/99/ DEBUG=true EXECUTOR_NUMBER=1 [email protected] GIT_AUTHOR_NAME=jenkinsbot GIT_BRANCH=pass_country_on_bankaccount GIT_COMMIT=bc1778116a147a6634191704922da33bd8cdc214 [email protected] GIT_COMMITTER_NAME=jenkinsbot GIT_PREVIOUS_COMMIT=bc1778116a147a6634191704922da33bd8cdc214 GIT_PREVIOUS_SUCCESSFUL_COMMIT=bc1778116a147a6634191704922da33bd8cdc214 [email protected]:example/com.git GOROOT=/usr/local/go/1.5.1 HOME=/var/lib/jenkins HUDSON_COOKIE=878e879b-d5bb-asda-b3b9-4e7b4d53basd HUDSON_HOME=/var/lib/jenkins HUDSON_SERVER_COOKIE=3fc7dd720e6d2e56 HUDSON_URL=http://jenkins.net:8080/ JENKINS_HOME=/var/lib/jenkins JENKINS_SCRIPTS=/var/lib/jenkins/workspace/jenkins-scripts/jenkins-scripts JENKINS_SERVER_COOKIE=3fc7dd720e6d2e56 JENKINS_URL=http://jenkins.net:8080/ JOB_NAME=build-on-PR JOB_URL=http://jenkins.net:8080/job/build-on-PR/ LANG=en_US.UTF-8 LC_COLLATE=C LOGNAME=jenkins MAIL=/var/mail/jenkins NLSPATH=/usr/dt/lib/nls/msg/%L/%N.cat NODE_LABELS=go master, NODE_NAME=master PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/local/go/bin:/var/lib/jenkins/go/bin PDIR=src/github.com/example/com PROJECTIMPORTPATH=github.com/example/com PROJECTNAME=ms-psps PWD=/var/lib/jenkins/workspace/build-on-PR ROOT_BUILD_CAUSE=GHPRBCAUSE ROOT_BUILD_CAUSE_GHPRBCAUSE=true SHELL=/bin/bash SHLVL=2 TERM=xterm-256color USER=jenkins WORKSPACE=/var/lib/jenkins/workspace/build-on-PR XDG_RUNTIME_DIR=/run/user/1000 XDG_SESSION_ID=23 XFILESEARCHPATH=/usr/dt/app-defaults/%L/Dt _=/usr/bin/env ghprbActualCommit=bc1778116a147a6634191704922da33bd8cdc214 ghprbActualCommitAuthor=Dev1 [email protected] ghprbAuthorRepoGitUrl=https://github.com/example/com.git ghprbCommentBody=test this please ghprbCredentialsId=6da1257c-1f41-23s1-asd2-03af2f772ad1 ghprbGhRepository=example/com ghprbPullAuthorLogin=Dev1 ghprbPullAuthorLoginMention=@Dev1 ghprbPullDescription=GitHub pull request #22 of commit bc1778116a147a6634191704922da33bd8cdc214, no merge conflicts. ghprbPullId=22 ghprbPullLink=https://github.com/example/com/pull/22 ghprbPullLongDescription=The original code works when ghprbPullTitle=Pass country code ghprbSourceBranch=pass_country ghprbTargetBranch=master ghprbTriggerAuthorLogin=Dev1 ghprbTriggerAuthorLoginMention=@Dev1 sha1=origin/pr/22/merge

kowalcj0 avatar Nov 30 '15 17:11 kowalcj0

Very strange. I guess I was wrong. I don't know what to tell you then.

DavidTanner avatar Nov 30 '15 17:11 DavidTanner

I am seeing the same issue in the current version 1.31.4, sometimes the rev-list is a different sha1 and used to set the GIT_COMMIT. My build fails because the rev-list sha1 is not valid:

git checkout -f cdcfcc934e72027a0f1ca8d544d350091abbcdf0 git rev-list adef4d900d68a74f93964bfae6374e37f8157747 # timeout=10

Switching to branch adef4d900d68a74f93964bfae6374e37f8157747... fatal: reference is not a tree: adef4d900d68a74f93964bfae6374e37f8157747

This happens every-so-often and telling the system to retest seems to work.

sonoble avatar Apr 22 '16 17:04 sonoble

exactly, me too. it happens now and then. We suffered from it very well....

Cloning repository https://github.com/RackHD/on-core.git
 > git init /var/lib/jenkins/jobs/on-core/workspace/build # timeout=10
Fetching upstream changes from https://github.com/RackHD/on-core.git
 > git --version # timeout=10
 > git -c core.askpass=true fetch --tags --progress https://github.com/RackHD/on-core.git +refs/heads/*:refs/remotes/origin/* +refs/pull/*:refs/remotes/origin/pr/*
 > git config remote.origin.url https://github.com/RackHD/on-core.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config --add remote.origin.fetch +refs/pull/*:refs/remotes/origin/pr/* # timeout=10
 > git config remote.origin.url https://github.com/RackHD/on-core.git # timeout=10
Fetching upstream changes from https://github.com/RackHD/on-core.git
 > git -c core.askpass=true fetch --tags --progress https://github.com/RackHD/on-core.git +refs/heads/*:refs/remotes/origin/* +refs/pull/*:refs/remotes/origin/pr/*
Seen branch in repository origin/master
.....................................
Seen 239 remote branches
Checking out Revision a5aba6637bcb9cf6c2492a1e4a43c66fd0cccb98 (origin/pr/196/merge)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f a5aba6637bcb9cf6c2492a1e4a43c66fd0cccb98
 > git rev-list 8232881895225ea48893418d9f0579e9da11aa2a # timeout=10

the $sha1 = origin/pr/196/merge

No idea what the "8232881895225ea48893418d9f0579e9da11aa2a" comes from, it doesn't exist .....

panpan0000 avatar Sep 01 '16 08:09 panpan0000

Is there any update on this issue?

abal5 avatar Mar 02 '17 19:03 abal5

It happens to our development recently, having Jenkins checkout a second last SHA of a given SHA. Jenkins: 1.523 on Linux git version 1.8.3.1

gordonchanhk avatar Feb 02 '18 05:02 gordonchanhk

We also have the same issue. Jenkins ver. 2.150.1 running on linux Ubuntu 14.04 Git plugin 3.9.1 Git ver: 2.15.0

The problem occurs when running Jenkins upon GitHub PR. The branch in the PR is updated with new commits by 'git push --force-with-lease'.

When working with a shallow fetch (20), the GIT_COMMIT is the same value like GIT_PREVIOUS_COMMIT so when using MultiJob - child is trying to checkout the wrong sha1 (which is GIT_PREVIOUS_COMMIT). During that run, 'fatal: reference is not a tree' error occurs in the child build. We found that the GIT_PREVIOUS_COMMIT value is a merged previous commit with a target branch during the PR. You can find those "test" commits in the github by using a url: https://github.com///commit/

When working without a shallow fetch, the problem not fixed and a parent calculates the GIT_PREVIOUS_COMMIT commit correctly but the GIT_COMMIT is a merge with some old commit on that PR with a target branch.

To workaround that problem, we DO NOT pass a "Pass-Through Git Commit that was build" to the child job, so the child job will calculate the sha1 by itself. Since the child job is not triggered by a PR, child doesn't have a 'sha1'. Just make sure to select the checkbox 'Current job parameters' in the parent job so the child will have a parameter 'sha1' to run a command: git rev-parse origin/${sha1}^{commit} git rev-parse ${sha1}^{commit}

Is there some fix planned for that bug? It was very time consuming to debug and find a work around.

Dancho80 avatar Apr 24 '19 07:04 Dancho80

Is there some fix planned for that bug? It was very time consuming to debug and find a work around.

i agree

camerondavison avatar Jul 15 '20 21:07 camerondavison

I can't get changes of the last commit using affectedFiles since jenkins workspace is not aware of the correct parent id. Rather, it sets it as the merge hash, which of course will give me all changes since then. When I tried to use${GIT_PREVIOUS_COMMIT} it gave me the same value as the current commit id! Anyone has a workaround?

Badrmoh avatar Jul 12 '21 11:07 Badrmoh

I found a workaround by setting "Branch Specifier" to ${ghprbActualCommit}. Reference: https://github.com/jenkinsci/ghprb-plugin#creating-a-job

victtsl avatar Jan 11 '22 19:01 victtsl