「Jenkins」- 多分支流水 | Multibranch Pipeline

properties({}) | 修改构建的配置

properties: Set job properties | https://www.jenkins.io/doc/pipeline/steps/workflow-multibranch/#-properties-%20set%20job%20properties

https://stackoverflow.com/questions/44113834/trigger-hourly-build-from-scripted-jenkinsfile

properties(
    [
        ...  , // other properties that you have
        pipelineTriggers([cron('0 * * * *')]),
    ]
)

场景 | 触发构建 | 针对分支 | Branch

Jenkins 2.469 | GitLab 16.5

通过 GitLab 触发 Multibranch Pipeline 构建。针对自动化触发,需要 GitLab Plugin 插件。https://plugins.jenkins.io/gitlab-plugin/

第一步、创建多分支流水

当创建 Job 实例时,选择 Multibranch Pipeline 即可,具体细节不再展开。

Pipeline: Multibranch | https://plugins.jenkins.io/workflow-multibranch/

第二步、配置自动化触发

在 GitLab 中,选择项目,Settings,Webhooks,Add new webhook

针对 URL 参数,需要使用 https://USERID:APITOKEN@JENKINS_URL/project/YOUR_JOB 格式,或使用 https://USERID:APITOKEN@JENKINS_URL/project/FOLDER/PROJECT_NAME 格式。但是,不要使用 https://JENKINS_URL/job/PROJECT_NAME/build 或 https://JENKINS_URL/job/gitlab-plugin/buildWithParameters 格式。

针对 Multibranch Pipeline 场景,似乎并不能在 Job 中创建独立的 Token 参数。

补充说明

针对 Multibranch Pipeline 场景,Manage Jenkins,GitLab,取消`Enable authentication for ‘/project’ end-point`选项,似乎并不能关闭认证。

场景 | 触发构建 | 针对标签 | Tag

In Jenkins, does a multibranch pipeline support Git tags? – Stack Overflow
When using tags in Jenkins Pipeline

在 Multibranch Pipeline 中,配置 Discover tags 参数,能够实现 Tag 自动发现。

但是,需要手动进行构建,暂时(09/10/2024,)无法自动触发。参考 [JENKINS-47496] No automatic builds for tags#comment-317789 的解释,其大致意思是:如果存在较多 Tag,那么首次触发将导致多个构建。

场景 | 放弃构建 | 第一次构建

首次创建 Pipeline 时,如果多个分支都包含 Jenkinsfile 文件,则会触发多个构建。

例如,我们 Fork 一个项目,该项目多个分支包含 Jenkinsfile 文件,则创建 Multibranch Pipeline 作业后,首次扫描将触发多个构建。

我们希望能够放弃首次构建。

# 09/10/2024 | 放弃首次构建是一种解决方案。还有其他方案,例如,分析问题的发生场景,或许我们并不需要处理该问题。

针对我们的场景,我们发布生产环境时,将创建对应的 Git 分支,而改分支仅构建一次。所以,我们无法禁用首次构建。如果禁用,需要人工干预。

# 04/24/2025 | Pipeline: Multibranch Version 803.v08103b_87c280 | 在 Branch Source 中,添加 Git 仓库,Property strategy,Add property

Suppress automatic SCM triggering
  • Branch names to build automatically: ^$
  • Suppression strategy: For matching branches schedule all builds (nothing is suppressed)

当增加该配置后,当 Pipeline Scan 时,发现新分支将不再自动进行构建。