在Jenkins Jenkinsfile中获取git分支名称 [英] Get git branch name in Jenkins Jenkinsfile
问题描述
我创建了一个jenkins管道,它从scm拉取管道脚本。
我将分支说明符设置为' all
',所以它建立在对任何分支的任何改变上。
如何从Jenkinsfile访问导致此构建的分支名称?
除了
sh(returnStdout :true,script:'git rev-parse --abbrev-ref HEAD')。trim()
它总是 master
。
您的管道,请检查您的执行时间是否在您的环境变量中看到了您的分支名称。
您可以使用以下方式打印它们:
pipeline {
代理任何
环境{
DISABLE_AUTH ='true'
DB_ENGINE ='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:
- Branch Conditional (see this groovy script)
- parallel branches pipeline (as seen by the OP)
这篇关于在Jenkins Jenkinsfile中获取git分支名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!