问题
I want to be able to say something like:
git branch: commitHash, credentialsId: credentialsId, url: url
The usecase: I'm doing parallel build and test runs on different platforms, and want to ensure each gets the same code. It is C++, and we build on separate platforms as well as building on them.
If I do the above, it fails - the underlying code assumes the given branch actually is a branch, or you get something like:
[Linux64 Build] > git rev-parse origin/e4b6c976a0a986c348a211579f1e8fd32cf29567^{commit} # timeout=10
[Pipeline] [Linux64 Build] }
[Pipeline] [Linux64 Build] // dir
[Pipeline] [Linux64 Build] }
[Pipeline] [Linux64 Build] // node
[Pipeline] [Linux64 Build] }
[Linux64 Build] Failed in branch Linux64 Build
I've seen variations on this question asked before, although no actual answers - just suggestions like to stash the source code instead, etc. Not really what I'm after.
The documentation suggests it ought to be possible to give explicit commit hashes, possibly using branches instead, but I can't work out the syntax and can't find any examples. When I do it, I get the master branch, I think - in our setup, master does not work.
So far, the only solution I've found has been to checkout the branch and then explicitly call git to get the commit:
git branch: branch, credentialsId: credentialsId, url: url
sh 'git checkout ' + commitHash
(where branch is the branch I originally got the hash for at the top of the job. It works but is not the neatest.
Anybody got a better way?
回答1:
Use a general scm step
checkout([$class: 'GitSCM', branches: [[name: commitHash ]],
userRemoteConfigs: [[url: 'http://git-server/user/repository.git']]])
回答2:
Yuri G's example didn't work for me when jenkins lacked a workspace due to initial checkout. The following works in this case. I don't understand why they are all that different.
def commitId = "<insert sha here>"
checkout ( [$class: 'GitSCM',
branches: [[name: commitId ]],
userRemoteConfigs: [[
credentialsId: 'deploy key for your repo',
url: 'repo url']]])
来源:https://stackoverflow.com/questions/43611673/jenkins-pipeline-checkout-explicit-git-commit