简体   繁体   English

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

[英]Jenkins: pass git commit hash to a downstream job

I have 2 jobs (A and B).我有 2 个工作(A 和 B)。 Job A points to a git repo at URL-A.作业 A 指向 URL-A 处的 git 存储库。 Job B points to a different git repo, at URL-B.作业 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 commit 哈希和一个 JAR 工件,它们将包装在 Docker 映像中并推送到 Docker 集线器)

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 插件设置了几个可以在脚本中使用的环境变量:

GIT_COMMIT - SHA of the current GIT_COMMIT - 当前的 SHA

GIT_BRANCH - Name of the remote repository (defaults to origin), followed by name of the branch currently being used, eg "origin/master" or "origin/foo" GIT_BRANCH - 远程存储库的名称(默认为 origin),后跟当前使用的分支的名称,例如“origin/master”或“origin/foo”

etc.等。

Just make sure to pass this parameter to Job B explicitly (predefined parameters, parameter from file, etc).只需确保将此参数显式传递给作业 B(预定义参数、来自文件的参数等)。

In Job B, check This project is parameterized .在作业 B 中,选中This project is parameterized Define a String Parameter , set Name to COMMIT for example.定义一个String Parameter ,例如将 Name 设置为COMMIT In Job A, set Post-build Actions with Trigger parameterized build on other projects .在作业 A 中,设置Post-build Actions with Trigger parameterized build on other projects Project to build is Job B's name, with Trigger when build is as Stable . Project to build是作业 B 的名称, Trigger when build isStable Trigger when build is Add Parameters with Predefined parameters , in which you just put COMMIT=$commit . Add Parameters带有Predefined parameters ,您只需在其中放置COMMIT=$commit $commit is 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. $commit通常是作业 A 中定义的参数。您可以在作业 B 中添加其他预定义参数,并将值传递给作业 A 中的全部或部分参数。

Easy way is to use Jenkins parameterized-trigger plugin 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 将接收该参数并进一步处理的位置

It seems that now (Parameterized trigger 2.37, Jenkins 2.204), "trigger parameterized build" has the option to "Pass-through Git Commit that was built".似乎现在(参数化触发器 2.37,Jenkins 2.204),“触发器参数化构建”可以选择“传递已构建的 Git 提交”。 Working even without any special configuration in the downstream job.即使在下游作业中没有任何特殊配置也能工作。

Using (Parameterized trigger Plugin https://plugins.jenkins.io/parameterized-trigger/ ) you set the following:使用(参数化触发器插件https://plugins.jenkins.io/parameterized-trigger/ )设置以下内容:

On Upstream JobA: Post-Build Actions -> Trigger parameterized build on other projects -> Project to build: JobB.在上游 JobA:构建后操作 -> 在其他项目上触发参数化构建 -> 要构建的项目:JobB。 Add Parameter -> Pass-through Git Commit that was build.添加参数 -> 构建的传递 Git 提交。

On Downstream JobB: Just Check Source Code Management from None to Git and set same repo as in JobA.在下游 JobB 上:只需检查源代码管理从 None 到 Git 并设置与 JobA 中相同的 repo。 For Branch just do nothing, you can even delete branch, but Jenkins will add master automatically.对于 Branch 什么都不做,你甚至可以删除分支,但是 Jenkins 会自动添加 master。 Don't worry regarding Master, this JobB will use GitCommit SHA from JobA不用担心 Master,这个 JobB 将使用 JobA 的 GitCommit SHA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM