简体   繁体   English

Jenkins 管道分支名称返回 null

[英]Jenkins pipeline branch name returns null

I'm trying to get the name of my branch for a jenkins groovy script.我正在尝试获取 jenkins groovy 脚本的分支名称。 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.这是 output - 它总是返回 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. BRANCH_NAME 对于多分支项目,这将设置为正在构建的分支的名称,例如,如果您希望从主分支而不是功能分支部署到生产环境。

I was testing in a normal pipline我在一个正常的管道中测试

In Jenkins there is two pipeline options:在 Jenkins 中有两个管道选项:

  1. New Item -> Pipeline --> env.BRANCH_NAME return branch null新项目 -> 管道 -> env.BRANCH_NAME返回分支null
  2. New Item -> Multibranch Pipeline --> env.BRANCH_NAME return branch master or branch name新项目 -> 多分支管道 -> env.BRANCH_NAME返回分支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.这将使 git repo 进入分离的 HEAD模式,就其本质而言,它没有分支。

From Jenkinsfile :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. scm是一个特殊变量,它指示检出步骤克隆触发此管道运行的特定修订。

So the ${env.BRANCH_NAME} is null.所以${env.BRANCH_NAME}为空。

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):如“ Jenkins Workflow Checkout Accessing BRANCH_NAME and GIT_COMMIT ”中所述,您可以使用 groovy 语法(在 Jenkins 管道 DSL 中进行调整)获取刚刚签出的 SHA1:

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}在管道作业中,我使用env.GIT_BRANCH解析为origin/{BRANCH}

In the case of a multibranch job, env.GIT_BRANCH resolves to {BRANCH} (no origin/ ).在多分支作业的情况下, env.GIT_BRANCH解析为{BRANCH} (无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 .我在Get git branch name in Jenkins Pipeline/Jenkinsfile上找到了这个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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