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)\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)
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.
When the Jenkins Git plugin clones a repo, it checks out a specific commit, rather than HEAD of the repo. This puts the repo in a "detached" state, so if you want to perform further git operations on the repo you need to attach to a branch with a checkout command in a shell step.
When you use the git checkout command to view a commit, you'll enter “detached HEAD state”. This refers to when you are viewing a commit that is not the most recent commit in a repository. Detached HEAD state is not an error nor is it a problem. When you are ready, you can navigate back to the HEAD in your repository.
Configuring Git with Jenkins Now open your project and go to configure. Step 2: Give the repository Url in Source Code Management, repository Url can be fetched by clicking on clone and download option of Github and you have to select the SSH Url. Also, add credentials there of Jenkins.
After adding a new git repository (project configuration > Source Code Management > check the GIT option ) to the project navigate to the bottom of the plugin settings, just above Repository browser region.
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)\Jenkins\workspace\Ensure>cd C:\Program Files (x86)\Jenkins\workspace\Ensure
C:\Program Files (x86)\Jenkins\workspace\Ensure>git branch
* feature/merge_tfs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With