【问题标题】:Accessing SCM (Git) variables on a Jenkins Pipeline job [duplicate]在 Jenkins Pipeline 作业中访问 SCM (Git) 变量 [重复]
【发布时间】:2016-03-08 17:23:34
【问题描述】:

这是我的管道代码:

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

构建结果如下:

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

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

【问题讨论】:

  • 在没有原始链接的情况下标记重复有什么意义???这对任何人都完全不酷和有用!!!!

标签: git jenkins jenkins-workflow


【解决方案1】:

以下是如何获取GIT_COMMIT 的示例(参考:Jenkins GitHub):

// These should all be performed at the point where you've
// checked out your sources on the agent. A 'git' executable
// must be available.
// Most typical, if you're not cloning into a sub directory
shortCommit = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()

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

【讨论】:

  • 您实际上无法扩展它来获取 GIT_BRANCH,因为 jenkins 以分离的头部状态签出。
  • 您可以使用更短的命令返回相同的结果git describe --always
  • 请将答案内嵌,而不是作为超链接。塔。
猜你喜欢
  • 1970-01-01
  • 2016-11-22
  • 1970-01-01
  • 1970-01-01
  • 2018-09-21
  • 2021-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多