本文介绍了在多分支管道上触发分支索引 (Jenkins/Git)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Jenkins 的多分支管道作业上自动触发分支索引".

I'm trying to automatically trigger 'Branch Indexing' on a Multibranch Pipelines job in Jenkins.

目前,似乎只有一种方法实际上有效,即轮询,但我无法做到这一点,而且轮询无论如何都是一个糟糕的解决方案.

At the moment, only one method seems to actually work, which is polling, but I am unable to do that and polling is a bad solution anyway.

该插件不支持远程触发构建(例如,从脚本)"(不保存选项),因此我无法通过推送等网络挂钩触发它.

The plug-in doesn't support 'Trigger builds remotely (e.g., from scripts)' (options are not saved), so I cannot trigger it via a web hook on push etc.

我尝试在 repo 上创建触发"自由式构建,但构建后操作 - 构建其他项目"声称 Multibranch Pipeline 项目不是可构建项目.

I tried creating a 'trigger' freestyle build on the repo but the 'Post-build Actions - Build other projects' claims the Multibranch Pipeline project is not a buildable project.

如果轮询是我可以做到这一点的唯一方法,那么我需要禁用自动 SCM 触发(否则我们在重新索引时会得到重复的构建),因为我需要在分支项目上启用 Web 挂钩触发.

If polling is the only way I can do this, then I need to disable automatic SCM triggering (otherwise we get duplicate builds when we re-index) because I'll need to enable web hook triggering on the branch projects.

但这不起作用,因为我正在通过分支项目中的管道脚本设置网络挂钩,并且您需要至少构建一次才能注册该属性.

But that doesn't work, because I'm setting up the web hook via a pipeline script in the branch project, and you need to have built it at least once to have that property registered.

我一直在兜圈子,所以希望我只是错过了一些明显的东西,但我们将不胜感激.

I've been going around in circles for a while, so hopefully I've just missed something obvious, but any help would be appreciated.

我想能够做到以下之一

  • 以某种方式将多分支项目作为下游项目触发

  • Somehow trigger the multi-branch project as a downstream project

轮询多分支项目,只构建之前没有构建过的分支项目

Poll the multibranch project, and only build branch projects which have not been built before

干杯

推荐答案

方法 ComputedFolder.scheduleBuild() 可以从 groovy 脚本中调用.

The method ComputedFolder.scheduleBuild() can be invoked from a groovy script.

我刚刚从另一个多分支管道项目中的 groovy 代码触发了一个多分支管道项目中的分支索引,然后触发了该项目中的下游构建.

I have just triggered branch indexing in one multibranch pipeline project from the groovy code in a different multibranch pipeline project, which is then triggering a downstream build in that project.

代码是这样的:

@NonCPS
void scanRepo(String downStreamProjectName) {
    Jenkins.instance.getItemByFullName(downStreamProjectName).scheduleBuild()
}
...
String downStreamProject = 'my-folder/my-multibranch-project'
String downStreamJob = "${downStreamProject}/${env.BRANCH_NAME}"
if (Jenkins.instance.getItemByFullName(downStreamJob) == null) {
    scanRepo(downStreamProject)
    while (Jenkins.instance.getItemByFullName(downStreamJob) == null) {
        sleep(1)
    }
}
build([job: downStreamJob, wait: false, quietPeriod: 0])

请注意,Jenkins.instance.getItemByFullName(downStreamProjectName)WorkflowMultiBranchProject,它不是 Serializable,因此需要注意.

Notice that Jenkins.instance.getItemByFullName(downStreamProjectName) is the WorkflowMultiBranchProject which is not Serializable, so some care needs to be taken.

这篇关于在多分支管道上触发分支索引 (Jenkins/Git)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:52