Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkinsfile: git rev-parse --abbrev-ref HEAD returns HEAD

I am new to jenkins/devops; I am following this example. When I locally do (from the terminal):

git rev-parse --abbrev-ref HEAD 

I get the current branch's name. However from within Jenkinsfile, in the logs I am getting:

HEAD 

Been researching online for a while and couldn't find the reason so far. What are potential causes for this outcome?

Additional Details

In my jenkinsfile, i am trying to get the current git branch's name (the one that triggered the webhook) and then pipe it inside 'git branch' command, so code is as follows:

pipeline { agent {     label 'ubuntu' } stages {     stage('check') {     steps {             script {                env.GIT_BRANCH_NAME=sh(returnStdout: true, script: "git rev-parse --abbrev-ref HEAD").trim()             }              sh 'echo BRANCH_NAME ${GIT_BRANCH_NAME}'             git branch: GIT_BRANCH_NAME, credentialsId: '******', url: 'https://*****/*****/*****.git'       } .... } 

In the line

sh 'echo BRANCH_NAME ${GIT_BRANCH_NAME}' 

Returns HEAD

I found a way around this using git name-rev --name-only HEAD and modified the script code to:

script {     env.GIT_BRANCH_PATH=sh(returnStdout: true, script: "git name-rev --name-only HEAD").trim()     env.GIT_BRANCH_NAME=GIT_BRANCH_PATH.split('remotes/origin/')[1] } 

Now I get the right branch name and the steps work, but I would rather have a less hacky way of doing things.

What is the best method to achieve what I want to achieve using best practices?

PS I am not using multi-branching pipeline and the requirements were to not use multi-branching.

like image 392
chad Avatar asked Nov 03 '17 14:11

chad


People also ask

What does git rev-parse -- Abbrev Ref head do?

git rev-parse --abbrev-ref HEADGives the current branch.

What is git rev-parse -- show toplevel?

From the git rev-parse man page: --show-toplevel Show the absolute path of the top-level directory.

How does git rev-parse work?

git rev-parse is an ancillary plumbing command primarily used for manipulation. One common usage of git rev-parse is to print the SHA1 hashes given a revision specifier. In addition, it has various options to format this output such as --short for printing a shorter unique SHA1.


2 Answers

I am probably late in responding, but there is potentially a simpler way. Ensure that you select "Check out to specific local branch" under additional behaviors within your Git configuration. This will ensure that git checks out to the exact branch you are tracking, and your original command "git rev-parse --abbrev-ref HEAD" will work fine.enter image description here

like image 156
Vijay Ramaswamy Avatar answered Oct 24 '22 15:10

Vijay Ramaswamy


The solution that I've found for this situation is:

checkout([$class: 'GitSCM', branches: [[name: '*/' + branch]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'LocalBranch', localBranch: "**"]], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'cred', url: '[email protected]:repofolder/repo.git']]]) 

The key here is [$class: 'LocalBranch', localBranch: "**"]. It allows to checkout a branch not a revision.

The source for it is taken from here.

like image 21
Oleksandr Bondarenko Avatar answered Oct 24 '22 13:10

Oleksandr Bondarenko