在Jenkins管道作业上访问SCM(Git)变量 [英] Accessing SCM (Git) variables on a Jenkins Pipeline job

查看:748
本文介绍了在Jenkins管道作业上访问SCM(Git)变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的管道代码:

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

构建结果如下所示:

 由用户anonymous 
[Pipeline]分配节点:启动
在/ var / lib / jenkins / jobs / test / workspace中的master上运行
[Pipeline]节点{
[Pipeline] git
> git rev-parse --is-inside-work-tree#timeout = 10
从远程Git仓库获取更改
> git config remote.origin.url https://acme/scm/app.git#timeout = 10
从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
检出版本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分支-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]} //节点
[Pipeline]分配节点:End
[Pipeline]流水线结束
完成:SUCCESS
pre>

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

解决方案

以下示例显示如何获取GIT_COMMIT - https://github.com/jenkinsci/pipeline-examples/blob/master /pipeline-examples/gitcommit/gitcommit.groovy



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


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?

解决方案

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

You can extend it to expose GIT_BRANCH as 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_BRANCH variable.

这篇关于在Jenkins管道作业上访问SCM(Git)变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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