如何从管道作业访问 git 分支名称? [英] How to access git branch name from pipeline job?
本文介绍了如何从管道作业访问 git 分支名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 Jenkins Pipeline 作业,它被配置为签出一个 git repo 和一个特定的本地分支.
I have a Jenkins Pipeline job which is configured to checkout a git repo and a specific local branch.
如何在我的 Jenkinsfile 中获取本地分支的名称?
我尝试加载 git jenkins 插件 env 属性但有不走运.
I tried to load the git jenkins plugin env properties but had no luck.
node {
checkout scm
echo "1 "+ env.GIT_LOCAL_BRANCH
echo "2 "+ env.GIT_BRANCH
}
两个值都是空"
推荐答案
我发现我可以从 checkout scm
获取返回值,并使用它来获取分支名称(和其他值)
I found that I can capture the return value from checkout scm
and use that to get the branch name (and other values)
def scmVars
node('api-sample-build') {
stage('Clone source code') {
scmVars = checkout scm
// scmVars contains the following values
// GIT_BRANCH=origin/mybranch
// GIT_COMMIT=fc8279a107ebaf806f2e310fce15a7a54238eb71
// GIT_PREVIOUS_COMMIT=6f2e319a1fc82707ebaf800fce15a7a54238eb71
// GIT_PREVIOUS_SUCCESSFUL_COMMIT=310fce159a1fc82707ebaf806f2ea7a54238eb71
// GIT_URL=https://stash.someworkplace.com/scm/poc/api-sample.git
}
stage('test scope') {
echo scmVars.GIT_BRANCH
}
}
通过在节点外定义变量,它可以在结帐后分阶段使用.
By defining the variable outside the node it is available in stages after the checkout.
这篇关于如何从管道作业访问 git 分支名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文