Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

git - Jenkins: GitSCM finishes the clone in a detached head state, how can I make sure that the correct branch name is checked out?

I wrote a Jenkins pipeline which clones a git repository and runs a MSBUILD build.

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']]])
}

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)

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}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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']]])

Notice the "**" in the localBranch extension.

Jenkins log:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...