Jenkins pipeline branch name returns null

后端 未结 7 692
滥情空心
滥情空心 2021-01-03 22:25

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\')
ch         


        
相关标签:
7条回答
  • 2021-01-03 22:56

    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/).

    0 讨论(0)
  • 2021-01-03 22:57

    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
    
    0 讨论(0)
  • 2021-01-03 22:59

    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()
    }
    
    0 讨论(0)
  • 2021-01-03 23:03

    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
                }   
            }
          } 
    \\...
    
    0 讨论(0)
  • 2021-01-03 23:11

    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
    0 讨论(0)
  • 2021-01-03 23:12

    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

    0 讨论(0)
提交回复
热议问题