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

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

问题描述

我创建了一个jenkins管道,它从scm拉取管道脚本。

我将分支说明符设置为' all ',所以它建立在对任何分支的任何改变上。

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



除了

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

它总是 master

解决方案

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



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

  pipeline {
代理任何

环境{
DISABLE_AUTH ='true'
DB_EN​​GINE ='sqlite'
}

阶段{
stage('Build'){
steps {
sh'printenv'
}
}
}
}

然而, PR 91 显示分支名称只在某些管道配置中设置:


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.

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

Everything I have tried echos out null except

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

which is always master.

解决方案

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'
            }
        }
    }
}

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

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

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