小编典典

Jenkins:GitSCM以独立状态完成克隆,我如何确保签出正确的分支名称?

jenkins

我编写了一个Jenkins管道,该管道克隆了一个git存储库并运行MSBUILD构建。

我使用GitSCM将存储库克隆到工作区中,如下所示:

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指向一个分离的头,我不明白为什么。

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参数,因此,我无法通过简单地运行它来修复它:

git checkout ${BRANCH_NAME}

因此,在启动MSBUILD步骤之前,如何确定HEAD指向分支名称?

我确定有人已经遇到这种情况并找到了解决方案。


阅读 1328

收藏
2020-07-25

共1个答案

小编典典

经过大量的研究,甚至与詹金斯的专业人员联系,并且经过我的多次尝试,我找到了解决该问题的方法。

以下代码解决了该问题:

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扩展。

詹金斯日志:

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
2020-07-25