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?

查看:19
本文介绍了Jenkins:GitSCM 以分离头状态完成克隆,我如何确保检出正确的分支名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在克隆 repo 的步骤发生后,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)Jenkinsworkspacebb
[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)Jenkinsworkspacebb # 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)Jenkinsworkspacebb>cd C:Program Files (x86)Jenkinsworkspacebb 

C:Program Files (x86)Jenkinsworkspacebb>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 扩展中的 "**".

詹金斯日志:

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)JenkinsworkspaceEnsure>cd C:Program Files (x86)JenkinsworkspaceEnsure 

C:Program Files (x86)JenkinsworkspaceEnsure>git branch 
* feature/merge_tfs

这篇关于Jenkins:GitSCM 以分离头状态完成克隆,我如何确保检出正确的分支名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆