Jenkins Workflow 插件中的 Git 变量

标签 git jenkins groovy jenkins-pipeline

当我在构建流的下方从 git checkout 存储库时,我想访问 git 变量,例如 GIT_COMMITGIT_BRANCH。目前我发现没有可用的变量来访问这两个参数。

node {
    git git+ssh://git.com/myproject.git
    echo "$GIT_COMMIT - $BRANCH_NAME"
}

这些变量是否可用,如果可以的话,我在哪里可以找到它们。我不介意它们是否可以通过某些 groovy 变量或任何地方使用,只要我可以访问它们即可。

也许我缺乏 Groovy 的调试技能,这很容易找到,但我的技能有限,找不到它。

最佳答案

根据您使用的 SCM 插件,checkout 步骤可能会返回有关修订的附加信息。这已在 JENKINS-26100 中解决.它在 2.6 version of the workflow-scm-step plugin 中发布.

例如,使用 Git 插件,您可以执行以下操作:

final scmVars = checkout(scm)
echo "scmVars: ${scmVars}"
echo "scmVars.GIT_COMMIT: ${scmVars.GIT_COMMIT}"
echo "scmVars.GIT_BRANCH: ${scmVars.GIT_BRANCH}"

这将根据您使用的插件而有所不同,因此原始答案可能更适合您。


原始答案

随着 Pipeline Nodes and Processes Plugin 的 2.4 版本发布,你可以简单地做:

def gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()

关于Jenkins Workflow 插件中的 Git 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35554983/

相关文章:

gradle - Ratpack + Thymeleaf + ShadowJar - 解析模板时出错 "home",模板可能不存在或无法访问

java - Play 框架中出现 "groovy.lang.MissingFieldException: No such field: metaClass for class: java.lang.Class"错误

Git merge 请求序列

git - 将 TAG 推送到服务器时发生 fatal error

c# - 如何从 Jenkins 的多个测试项目中获取 dotCover 覆盖率报告

jenkins - Jenkins 中的 "extended Email notification"和 "Email notification"有什么区别

git - Azure devops 显示其他人提交我的代码

mysql - 如何与 Git(或 à la Git)同时使用数据库

maven - 具有Maven,Jenkins,Nexus的基础架构

java - 如何使用 grails 中的现有 java 类