在多分支管道上触发分支索引(Jenkins/Git) [英] Triggering Branch Indexing on Multibranch Pipelines (Jenkins/Git)

查看:148
本文介绍了在多分支管道上触发分支索引(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.

我尝试在存储库上创建触发"自由式构建,但构建后操作-构建其他项目"声称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.

但是那是行不通的,因为我是通过分支项目中的管道脚本来设置Web钩子的,并且您至少需要构建一次以注册该属性.

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.

我想能够执行以下操作之一

I imagined being able to do one of the following

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

  • 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().

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

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

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)是不是SerializableWorkflowMultiBranchProject,因此需要格外小心.

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

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

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