Jenkins 管道:签出显式 git commit [英] Jenkins pipeline: checkout explicit git commit
问题描述
我希望能够说类似的话:
I want to be able to say something like:
git branch: commitHash, credentialsId: credentialsId, url: url
用例:我在不同平台上进行并行构建和测试运行,并希望确保每个平台都获得相同的代码.它是 C++,我们在不同的平台上构建以及构建在它们之上.
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.
文档建议应该可以给出明确的提交哈希,可能使用分支,但我无法计算出语法,也找不到任何示例.当我这样做时,我会得到 master 分支,我认为 - 在我们的设置中,master 不起作用.
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.
到目前为止,我找到的唯一解决方案是检查分支,然后显式调用 git 来获取提交:
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.
有人有更好的方法吗?
推荐答案
使用 通用 scm 步骤
checkout([$class: 'GitSCM', branches: [[name: commitHash ]],
userRemoteConfigs: [[url: 'http://git-server/user/repository.git']]])
这篇关于Jenkins 管道:签出显式 git commit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!