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

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

问题描述

我已经创建了一个詹金斯管道,它正在从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.

推荐答案

如果您的管道有jenkins文件,请检查在执行时是否在

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天全站免登陆