Jenkins:GitSCM在一个分离头状态下完成了克隆,我怎样才能确保 checkout 正确的分支名称?

kgqe7b3p  于 2023-01-28  发布在  Git
关注(0)|答案(4)|浏览(157)

我写了一个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']]])
}

在克隆repo的步骤发生之后,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 pipeline时,git参数不能正确求值,因此我不能简单地通过运行以下命令来修复它:

git checkout ${BRANCH_NAME}

那么,在开始MSBUILD步骤之前,如何确保HEAD指向分支名称呢?
我相信已经有人遇到这种情况,并有一个解决方案。

nwlqm0z1

nwlqm0z11#

经过大量的研究,甚至联系Jenkins专业人士和许多尝试,从我这边,我找到了如何解决这个问题。
以下代码修复了该问题:

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扩展名中的"**"
Jenkins日志:

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
vs3odd8k

vs3odd8k2#

添加[[$class: 'LocalBranch', localBranch: "**"]]修复了我的问题。
使用GitSCM插件成功检出主分支后,.git/refs/heads/master仍然缺失,导致某些操作始终失败(例如,git log --decorate ...创建变更日志)。

laik7k3q

laik7k3q3#

如果您想使用Pipeline配置实现相同的行为,请使用Pipeline-〉Additional Behaviours -〉Checkout to specific local分支-〉在输入字段中插入分支名称(或'**')。
My pipeline snapshot

gmol1639

gmol16394#

https://stackoverflow.com/a/61063557/6776354中所述,将checkout scm更新为

def scmVars = checkout([
                    $class: 'GitSCM',
                    branches: scm.branches,
                    extensions: scm.extensions, 
                    userRemoteConfigs: [[
                        credentialsId: scm.userRemoteConfigs[0].credentialsId,
                        name: 'origin', 
                        refspec: '+refs/heads/*:refs/remotes/origin/*', 
                        url: scm.userRemoteConfigs[0].url
                    ]],
                    doGenerateSubmoduleConfigurations: false
                ])

对我很有效。这确保了所有分支都可用。

相关问题