在 Jenkins Pipeline/Jenkinsfile 中获取 git 分支名称 [英] Get git branch name in Jenkins Pipeline/Jenkinsfile

查看:153
本文介绍了在 Jenkins Pipeline/Jenkinsfile 中获取 git 分支名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

除了

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

始终是master.

推荐答案

如果您的管道有 jenkinsfile,请检查在执行时是否在 环境变量.

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

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

You can print them with:

pipeline {
    agent any

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

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

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

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

  • Branch Conditional (see this groovy script)
  • parallel branches pipeline (as seen by the OP)

这篇关于在 Jenkins Pipeline/Jenkinsfile 中获取 git 分支名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆