如何使用 Jenkins Workflow 插件使 SCM 轮询工作 [英] How to make SCM polling work with the Jenkins Workflow plugin

查看:27
本文介绍了如何使用 Jenkins Workflow 插件使 SCM 轮询工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个普通的自由式项目中,我将 SCM 插件配置为指向我要发布的 Git 存储库,并启用Poll SCM"选项,这允许我配置一个 Stash webhook 以在有对该回购进行了更改.这样,只要将更改推送到 repo,就可以触发作业.

In a normal freestyle project, I configure the SCM plugin to point to the Git repo that I want to release, and I enable the "Poll SCM" option, which allows me to configure a Stash webhook to tell Jenkins whenever there has been a change to that repo. In this way, the job can be triggered whenever a change is pushed to the repo.

但是当我使用工作流而不是自由式项目时,我需要构建的代码的 SCM 在 groovy 工作流脚本中以编程方式指定,这意味着它没有监听 Stash webhook.相反,直接在工作流中配置的 SCM 是 groovy 脚本本身的 SCM,它与我尝试构建/发布的代码库不同,因此我不希望触发器基于此.

But when I use a workflow instead of a freestyle project, the SCM of the code that I need to build is specified programmatically in the groovy workflow script, which means that it is not listening for the Stash webhook. Instead, the SCM that is configured directly in the workflow is the SCM of the groovy script itself, which is different than the codebase that I am trying to build/release, so I don't want the trigger to be based on that.

node('docker_builder') {
    git url: serviceRepo
    releaseVersion = getVersion()
    pipelineSpec = getPipelineSpec()
    sh "./gradlew clean build pushDockerImage"
}

关于在使用工作流插件时如何实现 SCM 轮询的任何想法?

Any ideas about how to achieve SCM polling when using the workflow plugin?

推荐答案

我已经通过大量的研究和实验解决了这个问题.这份文档让我走上了正轨:https://github.com/jenkinsci/workflow-scm-step-plugin/blob/master/README.md.它说:

I have resolved this question with lots of research and experimentation. This documentation got me on the right track: https://github.com/jenkinsci/workflow-scm-step-plugin/blob/master/README.md. It says:

支持跨多个 SCM 进行轮询(更改一个或多个将触发新的构建),并且再次根据工作流的最后构建中使用的 SCM 完成."

Polling is supported across multiple SCMs (changes in one or more will trigger a new build), and again is done according to the SCMs used in the last build of the workflow."

这意味着 Jenkins 工作流程仍然支持 SCM 轮询,但与普通的自由式项目不同,您必须手动运行一次,然后它才能开始侦听 SCM 更改.这是有道理的,因为 SCM 是在 Groovy 代码中定义的;它们只有在运行一次后才会被发现.

This means that SCM polling is still supported with a Jenkins workflow, but unlike a normal freestyle project, you have to run it once manually before it starts listening for SCM changes. This makes sense because the SCM's are defined in Groovy code; they are not known until they run once.

其中一个棘手的元素是您可以在工作流程中定义许多 SCM.例如,我有三个:一个用于服务本身、一个部署脚本和 Groovy 工作流 DSL.默认情况下,对这三个 SCM 中的任何一个的更改都会导致SCM poll"选项触发构建,这可能是不可取的.幸运的是,在 Groovy 代码中的git"步骤中设置poll: false"选项将禁用对该 repo 的轮询.如果您正在从 SCM 读取 Groovy DSL,则可以通过单击 Jenkins UI 中的其他行为"并添加不触发提交通知构建"来禁用对该存储库的轮询.

One tricky element of this is that you can define many SCM's in your workflow. For example, I have three: one for the service itself, a deployment script, and the Groovy workflow DSL. By default, changes to any of those three SCM's would cause the "SCM poll" option to trigger a build, which may not be desirable. Luckily, setting the "poll: false" option on the "git" step in the Groovy code will disable polling on that repo. If you are reading your Groovy DSL from an SCM, then you can disable polling on that repo by clicking "additional behaviors" in the Jenkins UI and adding "Don't trigger a build on commit notifications".

另一个棘手的元素是 Stash web hook 插件默认情况下在 Jenkins 使用的 RESTful URL 中包含提交的 SHA1 哈希码.不幸的是,当 Jenkins 尝试提取您可能定义的多个 SCM 中的任何一个时,它使用相同的提交代码是错误的.哈希码当然只与一个 SCM 相关,因此它会中断.您可以通过在 Stash web hook 插件中设置省略 SHA1 哈希码"来解决此问题.然后 Jenkins 将只使用您在每个 SCM 中构建的任何分支上的最新提交.

Another tricky element is that the Stash web hook plugin by default includes the SHA1 hash code of the commit in the RESTful URL that it hits Jenkins with. Unfortunately, Jenkins makes the mistake of using that same commit code when it tries to pull any of the multiple SCM's that you may have defined. The hashcode is of course only relevant to one SCM, so it breaks. You can get around this by setting "Omit SHA1 Hash Code" in the Stash web hook plugin. Then Jenkins will just use the latest commit on whatever branch you build from in each of your SCM's.

这篇关于如何使用 Jenkins Workflow 插件使 SCM 轮询工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆