我写了一个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指向分支名称呢?
我相信已经有人遇到这种情况,并有一个解决方案。
4条答案
按热度按时间nwlqm0z11#
经过大量的研究,甚至联系Jenkins专业人士和许多尝试,从我这边,我找到了如何解决这个问题。
以下代码修复了该问题:
注意
localBranch
扩展名中的"**"
。Jenkins日志:
vs3odd8k2#
添加
[[$class: 'LocalBranch', localBranch: "**"]]
修复了我的问题。使用
GitSCM
插件成功检出主分支后,.git/refs/heads/master
仍然缺失,导致某些操作始终失败(例如,git log --decorate ...
创建变更日志)。laik7k3q3#
如果您想使用Pipeline配置实现相同的行为,请使用Pipeline-〉Additional Behaviours -〉Checkout to specific local分支-〉在输入字段中插入分支名称(或'**')。
My pipeline snapshot
gmol16394#
如https://stackoverflow.com/a/61063557/6776354中所述,将
checkout scm
更新为对我很有效。这确保了所有分支都可用。