Jenkins管道:checkout显式git commit

joh*_*nfo 19 jenkins jenkins-pipeline

我想能够说出类似的话:

git branch: commitHash, credentialsId: credentialsId, url: url
Run Code Online (Sandbox Code Playgroud)

用例:我在不同的平台上进行并行构建和测试运行,并希望确保每个都获得相同的代码.它是C++,我们构建在不同的平台上以及构建它们.

如果我执行上述操作,则会失败 - 底层代码假定给定分支实际上是分支,或者您得到类似的内容:

[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
Run Code Online (Sandbox Code Playgroud)

我之前已经看到过这个问题的变化,虽然没有实际的答案 - 只是建议比如隐藏源代码,等等.不是我真正想要的.

文档建议应该可以提供显式提交哈希,可能使用分支,但我无法解决语法,也找不到任何示例.当我这样做时,我会得到主分支,我认为 - 在我们的设置中,主人不起作用.

到目前为止,我发现的唯一解决方案是检查分支,然后显式调用git来获取提交:

                git branch: branch, credentialsId: credentialsId, url: url
                sh 'git checkout ' + commitHash
Run Code Online (Sandbox Code Playgroud)

(其中branch是我最初在作业顶部获得哈希的分支.它可以工作,但不是最好的.

有人有更好的方法吗?

Yur*_* G. 24

使用一般的scm步骤

checkout([$class: 'GitSCM', branches: [[name: commitHash ]],
     userRemoteConfigs: [[url: 'http://git-server/user/repository.git']]])
Run Code Online (Sandbox Code Playgroud)

  • 我想我设法解决了 - 尤其是我刚刚注意到有一个代码片段"编辑器".它将类似于:checkout([$ class:'GitSCM',branches:[[name:commitHash]],userRemoteConfigs:[[credentialsId:credentialsId,url:'http://git-server/user/repository.git "]]]) (3认同)
  • 您是否知道如何添加credentialsId 位? (2认同)
  • 嗯,这项工作提供了之前发生的克隆.否则,如果提交ID不在refspec中,则无法找到.所以,如果它是标签或分支的HEAD,一切都很好.如果没有,它就会失败 (2认同)

Pet*_*ahn 10

当jenkins由于初步结账而缺少工作空间时,Yuri G的例子对我不起作用.在这种情况下,以下工作.我不明白他们为什么会那么不同.

    def commitId = "<insert sha here>"

    checkout ( [$class: 'GitSCM',
        branches: [[name: commitId ]],
        userRemoteConfigs: [[
            credentialsId: 'deploy key for your repo', 
            url: 'repo url']]])
Run Code Online (Sandbox Code Playgroud)