在 Jenkins Pipeline 作业上访问 SCM (Git) 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35873902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:59:38  来源:igfitidea点击:

Accessing SCM (Git) variables on a Jenkins Pipeline job

gitjenkinsjenkins-workflow

提问by Henrique Gontijo

Here's my pipeline code:

这是我的管道代码:

node ('master') {
    git url: "$GIT_REPO_URL", branch: "$GIT_BRANCH"
    echo env.GIT_COMMIT
    echo env.GIT_BRANCH
    echo env.GIT_REVISION
}

The build results looks like:

构建结果如下所示:

Started by user anonymous
[Pipeline] Allocate node : Start
Running on master in /var/lib/jenkins/jobs/test/workspace
[Pipeline] node {
[Pipeline] git
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://acme/scm/app.git # timeout=10
Fetching upstream changes from https://acme/scm/app.git
 > git --version # timeout=10
 > git fetch --tags --progress https://acme/scm/app.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision fb455725db1b768ff63e627a087d2771099af7c4 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f fb455725db1b768ff63e627a087d2771099af7c4 # timeout=10
 > git branch -a -v --no-abbrev # timeout=10
 > git branch -D master # timeout=10
 > git checkout -b master fb455725db1b768ff63e627a087d2771099af7c4
 > git rev-list fb455725db1b768ff63e627a087d2771099af7c4 # timeout=10
[Pipeline] echo
null
[Pipeline] echo
null
[Pipeline] echo
null
[Pipeline] } //node
[Pipeline] Allocate node : End
[Pipeline] End of Pipeline
Finished: SUCCESS

The env variables env.GIT_COMMIT, env.GIT_BRANCH are not populated. Are those values available in another variables?

env 变量 env.GIT_COMMIT、env.GIT_BRANCH 未填充。这些值是否在其他变量中可用?

回答by Chida

Here is an example of how you can get GIT_COMMIT- https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/gitcommit/gitcommit.groovy

这是您如何获得的示例GIT_COMMIT- https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/gitcommit/gitcommit.groovy

You can extend it to expose GIT_BRANCHas well. This script is from the workflow examples git repo managed by cloudbees. Maybe you can send a pull request if you add capability to retrieve GIT_BRANCHvariable.

您也可以将其扩展为公开GIT_BRANCH。此脚本来自由 cloudbees 管理的工作流示例 git repo。如果您添加检索GIT_BRANCH变量的功能,也许您可​​以发送拉取请求。