简体   繁体   中英

Jenkins pipeline branch name returns null

I'm trying to get the name of my branch for a jenkins groovy script. I cannot get the current branch name. I try the following:

stage('Check out code')
checkout scm
echo "My branch is: ${env.BRANCH_NAME}"

Here is the output - it always returns null.

 Checking out Revision 33b531b2f1caaf8b64d968e437306f39d2dba1da   (origin/pipeline)
  > git.exe config core.sparsecheckout # timeout=10
  > git.exe checkout -f 33b531b2f1caaf8b64d968e437306f39d2dba1da
 [Pipeline] echo
 My branch is: null

Am I missing something?

This variable only works in a multibranch pipline:

BRANCH_NAME For a multibranch project, this will be set to the name of the branch being built, for example in case you wish to deploy to production from master but not from feature branches.

I was testing in a normal pipline

In Jenkins there is two pipeline options:

  1. New Item -> Pipeline --> env.BRANCH_NAME return branch null
  2. New Item -> Multibranch Pipeline --> env.BRANCH_NAME return branch master or branch name

My workaround, Don't know if work for someone else..

def branchName = getCurrentBranch()
echo 'My branch is' + branchName

def getCurrentBranch () {
    return sh (
        script: 'git rev-parse --abbrev-ref HEAD',
        returnStdout: true
    ).trim()
}
git.exe checkout -f 33b531b2f1caaf8b64d968e437306f39d2dba1da

That would make the git repo enter a detached HEAD mode , which, by its very nature, has no branch.

From Jenkinsfile :

The checkout step will checkout code from source control; scm is a special variable which instructs the checkout step to clone the specific revision which triggered this Pipeline run.

So the ${env.BRANCH_NAME} is null.

As mentioned in " Jenkins Workflow Checkout Accessing BRANCH_NAME and GIT_COMMIT ", you can get the SHA1 you just checked out with the groovy syntax (to be adapted in a Jenkins pipeline DSL):

sh 'git rev-parse HEAD > commit'
def commit = readFile('commit').trim()

In the pipeline job I'm using env.GIT_BRANCH which resolves to origin/{BRANCH}

In the case of a multibranch job, env.GIT_BRANCH resolves to {BRANCH} (no origin/ ).

I had this same issue, but I resolved it by changing

println "${env.BRANCH_NAME}"

to

println "${BRANCH_NAME}"

Note my plugin is also checking out in detached mode:

git checkout -f e10a170e17fb5f9282f903a7b3cd17bd2e181dee

From a simple pipeline you can use the following script:

\\...
stage('Test') {
        steps {
            script {
                branchName = sh(label: 'getBranchName', returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()
                println branchName
            }   
        }
      } 
\\...

this code worked for me.

def BRANCH_NAME = getCurrentBranch()
echo 'Current branch is' + BRANCH_NAME
def getCurrentBranch () {
    return sh (
    script: 'git name-rev --name-only HEAD',
    returnStdout: true
    ).trim()
}

The following worked for me:

env.BRANCH_NAME = scm.branches[0].name

I have found this answer on Get git branch name in Jenkins Pipeline/Jenkinsfile .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM