在 Jenkins Pipeline/Jenkinsfile 中获取 git 分支名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42383273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:49:57  来源:igfitidea点击:

Get git branch name in Jenkins Pipeline/Jenkinsfile

gitjenkinsbranchjenkins-pipeline

提问by Alex Yurkowski

I've create a jenkins pipeline and it is pulling the pipeline script from scm.
I set the branch specifier to 'all', so it builds on any change to any branch.

我创建了一个 jenkins 管道,它正在从 scm 中提取管道脚本。
我将分支说明符设置为“ all”,因此它基于对任何分支的任何更改。

How do I access the branch name causing this build from the Jenkinsfile?

如何从 Jenkinsfile 访问导致此构建的分支名称?

Everything I have tried echos out null except

除了

sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()

which is always master.

这总是master.

采纳答案by VonC

If you have a jenkinsfile for your pipeline, check if you see at execution time your branch name in your environment variables.

如果您的管道有 jenkinsfile,请检查您在执行时是否在环境变量中看到您的分支名称。

You can print them with:

您可以使用以下方法打印它们:

pipeline {
    agent any

    environment {
        DISABLE_AUTH = 'true'
        DB_ENGINE    = 'sqlite'
    }

    stages {
        stage('Build') {
            steps {
                sh 'printenv'
            }
        }
    }
}

However, PR 91shows that the branch name is only set in certain pipeline configurations:

但是,PR 91显示分支名称仅在某些管道配置中设置:

回答by James

Use multibranch pipeline.. not pipeline

使用多分支管道..不是管道

In my script..

在我的脚本中..

stage('Build') {
    node {
        echo 'Pulling...' + env.BRANCH_NAME
        checkout scm

    }
}

Yields...

产量...

Pulling...master

回答by Attila123

A colleague told me to use scm.branches[0].nameand it worked. I wrapped it to a function in my Jenkinsfile:

一位同事告诉我使用scm.branches[0].name,它起作用了。我将它包装到我的 Jenkinsfile 中的一个函数中:

def getGitBranchName() {
    return scm.branches[0].name
}

回答by alex

For me this worked: (using Jenkins 2.150, using simple Pipeline type - not multibranch, my branch specifier: '**')

对我来说这有效:(使用 Jenkins 2.150,使用简单的管道类型 - 不是多分支,我的分支说明符:'**')

echo 'Pulling... ' + env.GIT_BRANCH

Output:

输出:

Pulling... origin/myBranch

where myBranch is the name of the feature branch

其中 myBranch 是功能分支的名称

回答by Alex Yurkowski

Switching to a multibranch pipeline allowed me to access the branch name. A regular pipeline was not advised.

切换到多分支管道允许我访问分支名称。不建议使用常规管道。

回答by user3437877

This is for simple Pipeline type - not multibranch. Using Jenkins 2.150.1

这是用于简单的管道类型 - 不是多分支。使用詹金斯 2.150.1

environment { FULL_PATH_BRANCH = "${sh(script:'git name-rev --name-only HEAD', returnStdout: true)}" GIT_BRANCH = FULL_PATH_BRANCH.substring(FULL_PATH_BRANCH.lastIndexOf('/') + 1, FULL_PATH_BRANCH.length()) }

environment { FULL_PATH_BRANCH = "${sh(script:'git name-rev --name-only HEAD', returnStdout: true)}" GIT_BRANCH = FULL_PATH_BRANCH.substring(FULL_PATH_BRANCH.lastIndexOf('/') + 1, FULL_PATH_BRANCH.length()) }

then use it env.GIT_BRANCH

然后使用它 env.GIT_BRANCH

回答by 0neel

Just getting the name from scm.branchesis not enough if you've used a build parameter as a branch specifier, e.g. ${BRANCH}. You need to expand that string into a real name:

scm.branches如果您将构建参数用作分支说明符,例如,仅获取名称是不够的${BRANCH}。您需要将该字符串扩展为真实姓名:

scm.branches.first().getExpandedName(env.getEnvironment())

Note that getEnvironment()must be an explicit getter otherwise envwill look up for an environment variable called environment.

请注意,getEnvironment()必须是显式 getter,否则env将查找名为environment的环境变量。

Don't forget that you need to approve those methods to make them accessible from the sandbox.

不要忘记您需要批准这些方法才能从沙箱访问它们。