在Jenkins Pipeline任务中访问SCM(Git)变量

25

这是我的管道代码:

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 没有被填充。这些值是否可以在其他变量中获取?


10
没有提供原始链接的情况下标记重复有什么意义?这完全毫无意义和用处!!! - U.V.
1个回答

25

以下是一个示例,展示如何获取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 存储库。如果你添加了检索 GIT_BRANCH 变量的功能,也许可以发送一个 pull 请求。


1
你实际上无法扩展此功能以获取GIT_BRANCH,因为Jenkins在分离的HEAD状态下检出。 - aflat
1
你可以使用一个更短的命令来返回相同的结果 git describe --always - Romain
4
好的,我会尽力满足您对翻译的要求。以下是需要翻译的内容:
  1. What are the most popular programming languages in 2021?
  • 2021年最流行的编程语言是什么?
  1. Can you recommend a good Chinese restaurant in the area?
  • 你能推荐附近一家好的中餐馆吗?
  1. How do I say "thank you" in Spanish?
  • 我该如何用西班牙语说“谢谢”?
  1. What is the capital of Australia?
  • 澳大利亚的首都是什么?
  1. Could you please provide me with an update on the project status?
  • 请问能否提供一下项目进展情况的更新?
- Banoona

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接