Jenkins:将 git commit hash 传递给下游作业

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37438600/
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-19 12:01:45  来源:igfitidea点击:

Jenkins: pass git commit hash to a downstream job

gitjenkins

提问by Elifarley

I have 2 jobs (A and B). Job A points to a git repo at URL-A. Job B points to a different git repo, at URL-B.

我有 2 个工作(A 和 B)。作业 A 指向 URL-A 处的 git 存储库。作业 B 指向不同的 git 存储库,位于 URL-B。

Once job A finishes and is stable, it should trigger job B and pass the git commit hash that was used in job A's build.

一旦作业 A 完成并稳定,它应该触发作业 B 并传递作业 A 构建中使用的 git commit 哈希。

What is the simplest way to achieve that?

实现这一目标的最简单方法是什么?

(Job B corresponds to a script checked out from URL-B that has 2 parameters: a git commit hash and a JAR artifact that will be wrapped in a Docker image and pushed to Docker hub)

(作业 B 对应于从 URL-B 检出的脚本,它有 2 个参数:一个 git 提交哈希和一个 JAR 工件,它们将被包装在 Docker 镜像中并推送到 Docker 集线器)

回答by Dvir669

When you checking out from Job A using the GIT plugin you already have a $GIT_COMMIT parameter that the Plugin exports:

当您使用 GIT 插件从作业 A 签出时,您已经拥有插件导出的 $GIT_COMMIT 参数:

Environment variables

The git plugin sets several environment variables you can use in your scripts:

GIT_COMMIT - SHA of the current

GIT_BRANCH - Name of the remote repository (defaults to origin), followed by name of the branch currently being used, e.g. "origin/master" or "origin/foo"

etc.

环境变量

git 插件设置了几个可以在脚本中使用的环境变量:

GIT_COMMIT - 当前的 SHA

GIT_BRANCH - 远程存储库的名称(默认为 origin),后跟当前使用的分支的名称,例如“origin/master”或“origin/foo”

等等。

Just make sure to pass this parameter to Job B explicitly (predefined parameters, parameter from file, etc).

只需确保将此参数显式传递给作业 B(预定义参数、来自文件的参数等)。

回答by ElpieKay

In Job B, check This project is parameterized. Define a String Parameter, set Name to COMMITfor example. In Job A, set Post-build Actionswith Trigger parameterized build on other projects. Project to buildis Job B's name, with Trigger when build isas Stable. Add Parameterswith Predefined parameters, in which you just put COMMIT=$commit. $commitis usually a paramter defined in Job A. You could add other predefined parameters in Job B and pass a value to all or some of them in Job A.

在作业 B 中,检查This project is parameterized。定义一个String ParameterCOMMIT例如将名称设置为。在作业 A 中,设置Post-build ActionsTrigger parameterized build on other projectsProject to build是作业 B 的名称,带有Trigger when build isas StableAdd ParametersPredefined parameters,您刚刚将COMMIT=$commit. $commit通常是作业 A 中定义的参数。您可以在作业 B 中添加其他预定义参数,并将值传递给作业 A 中的全部或部分参数。

回答by Anvesh Kumar

Easy way is to use Jenkins parameterized-triggerplugin by which you can trigger a build only on previous build is stable.

简单的方法是使用Jenkins 参数化触发器插件,通过它您可以仅在之前的构建稳定时触发构建。

You can pass the parameters as well. Like In your case set the git commit hash to parameter and call the Job B.

您也可以传递参数。就像在您的情况下,将 git commit hash 设置为参数并调用作业 B。

Where Job B would receive that parameter and process further

作业 B 将接收该参数并进一步处理的位置