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.