Jenkins:GitSCM以独立状态完成克隆,我如何确保签出正确的分支名称? [英] Jenkins: GitSCM finishes the clone in a detached head state, how can I make sure that the correct branch name is checked out?
问题描述
我编写了一个Jenkins管道,该管道克隆了一个git存储库并运行MSBUILD构建.
I wrote a Jenkins pipeline which clones a git repository and runs a MSBUILD build.
我使用GitSCM将存储库克隆到工作区中,如下所示:
I use GitSCM to clone the repository into the workspace like so:
stage ('Checkout SCM & Merge master to feature branch') {
checkout([$class: 'GitSCM', branches: [[name: '*/feature/*']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '99f978af-XXXX-XXXX-8147-2cf8f69ef864', url: 'http://TFS_SERVER:8080/tfs/DefaultCollection/PC_International/_git/REPO_NAME']]])
}
克隆回购步骤之后,HEAD指向一个分离的头部,我不明白为什么.
After the step of cloning the repo takes place, HEAD is pointing to a detached head and I don't understand why.
Started by user itai ganot
[Pipeline] node
Running on master in C:\Program Files (x86)\Jenkins\workspace\bbb
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Setup)
[Pipeline] deleteDir
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Checkout SCM & Merge master to feature branch)
[Pipeline] checkout
Cloning the remote Git repository
Cloning repository http://pctfs1:8080/tfs/DefaultCollection/PC_International/_git/Ensure-pcs-intl
> git.exe init C:\Program Files (x86)\Jenkins\workspace\bbb # timeout=10
Fetching upstream changes from http://TFS_SERVER:8080/tfs/DefaultCollection/PC_International/_git/Ensure-pcs-intl
> git.exe --version # timeout=10
using GIT_SSH to set credentials javab SSH file
> git.exe fetch --tags --progress http://TFS_SERVER:8080/tfs/DefaultCollection/PC_International/_git/REPO_NAME +refs/heads/*:refs/remotes/origin/*
> git.exe config remote.origin.url http://TFS_SERVER:8080/tfs/DefaultCollection/PC_International/_git/REPO_NAME # timeout=10
> git.exe config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
> git.exe config remote.origin.url http://TFS_SERVER:8080/tfs/DefaultCollection/PC_International/_git/REPO_NAME # timeout=10
Fetching upstream changes from http://TFS_SERVER:8080/tfs/DefaultCollection/PC_International/_git/REPO_NAME
using GIT_SSH to set credentials javab SSH file
> git.exe fetch --tags --progress http://TFS_SERVER:8080/tfs/DefaultCollection/PC_International/_git/REPO_NAME +refs/heads/*:refs/remotes/origin/*
Seen branch in repository origin/feature/merge_tfs
Seen branch in repository origin/master
Seen branch in repository origin/origin
Seen 3 remote branches
> git.exe tag -l # timeout=10
Checking out Revision 97b3493db4f726e11e334e5ba34fa808b63edec5 (origin/feature/merge_tfs)
> git.exe config core.sparsecheckout # timeout=10
> git.exe checkout -f 97b3493db4f726e11e334e5ba34fa808b63edec5
First time build. Skipping changelog.
[Pipeline] bat
[bbb] Running batch script
C:\Program Files (x86)\Jenkins\workspace\bbb>cd C:\Program Files (x86)\Jenkins\workspace\bbb
C:\Program Files (x86)\Jenkins\workspace\bbb>git branch
* (HEAD detached at 97b3493)
除此之外,众所周知,在运行Jenkins管道时,无法正确评估git参数,因此,我无法通过简单地运行它来修复它:
More than that, it is known that when running a Jenkins pipeline, git parameters are not evaluated correctly and because of that I can't fix it by simply running:
git checkout ${BRANCH_NAME}
那么在启动MSBUILD步骤之前,如何确保HEAD指向分支名称?
So how can I make sure before starting the MSBUILD step that HEAD is pointing to the branch name?
我确定有人已经遇到这种情况并找到了解决方案.
I'm sure someone has already bumped into this situation and has a solution.
推荐答案
经过大量的研究,甚至与Jenkins专业人员联系,并尝试了许多尝试,我找到了解决该问题的方法.
After lots of researching and even contacting Jenkins professionals and many tries from my side, I found how to solve this issue.
以下代码解决了该问题:
The following code fixes the issue:
checkout([$class: 'GitSCM', branches: [[name: '*/feature/*']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'LocalBranch', localBranch: "**"]], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '99f978af-XXXX-XXXX-8147-2cf8f69ef864', url: 'http://TFS_SERVER:8080/tfs/DefaultCollection/Product/_git/Project']]])
请注意localBranch扩展名中的"**".
Notice the "**" in the localBranch extension.
詹金斯记录:
Checking out Revision 97b3493db4f726e11e33XXXba34fa808b63edec5 (origin/feature/merge_tfs)
> git.exe config core.sparsecheckout # timeout=10
> git.exe checkout -f 97b3493db4f726e11e33XXXba34fa808b63edec5
> git.exe branch -a -v --no-abbrev # timeout=10
> git.exe checkout -b feature/merge_tfs 97b3493db4f726e11e33XXXba34fa808b63edec5
> git.exe rev-list 97b3493db4f726e11e334e5ba34fa808b63edec5 # timeout=10
[Pipeline] bat
[Ensure] Running batch script
C:\Program Files (x86)\Jenkins\workspace\Ensure>cd C:\Program Files (x86)\Jenkins\workspace\Ensure
C:\Program Files (x86)\Jenkins\workspace\Ensure>git branch
* feature/merge_tfs
这篇关于Jenkins:GitSCM以独立状态完成克隆,我如何确保签出正确的分支名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!