如何禁用分支索引中的触发器但仍允许 SCM 在多分支作业中触发

How to disable triggers from branch indexing but still allow SCM triggering in multibranch jobs

使用 Jenkins 多分支管道作业时 如果您在作业中选择 Suppress Automatic SCM trigger,它将在索引分支后停止作业构建(强大的功能)。

然而,出于某种原因,这也会破坏从 SCM 事件触发构建的能力!

有什么方法可以阻止构建在分支发现(分支索引)后触发,但仍然可以通过 SCM 事件正常构建?

这不是这个的功能 - https://issues.jenkins-ci.org/browse/JENKINS-41980

Suppressing SCM Triggers should suppress all builds triggered by a detected change in the SCM irrespective of how the change was detected

据我了解,发生这种情况是因为在设置 "Suppress Automatic SCM trigger" 时未读取管道定义。

因此,在您第一次 运行 作业之前,您在管道中声明的所有触发器(SCM、上游...)都不会被 Jenkins 知道。

因此,如果您不想通过分支索引触发构建,请设置选项 "Suppress Automatic SCM trigger" 如果你想让 Jenkins 知道你的管道,以便他可以对你的触发器做出反应,你不应该设置 "Suppress Automatic SCM trigger"

您始终可以向管道添加逻辑以在分支索引原因时中止。例如:

  boolean isBranchIndexingCause() {
    def isBranchIndexing = false
    if (!currentBuild.rawBuild) {
      return true
    }

    currentBuild.rawBuild.getCauses().each { cause ->
      if (cause instanceof jenkins.branch.BranchIndexingCause) {
        isBranchIndexing = true
      }
    }
    return isBranchIndexing
  }

调整逻辑以适应您的 use-case。

编辑:Jenkins UI 中嵌入的流水线语法 > 全局变量参考(例如:<jenkins url>/job/<pipeline job name>/pipeline-syntax/globals)包含有关 currentBuild 全局变量的信息,这会导致一些 javadoc:

The currentBuild variable, which is of type RunWrapper, may be used to refer to the currently running build. It has the following readable properties:

...

rawBuild:

a hudson.model.Run with further APIs, only for trusted libraries or administrator-approved scripts outside the sandbox; the value will not be Serializable so you may only access it inside a method marked @NonCPS

...

另请参阅:jenkins.branch.BranchIndexingCause

我知道这个 post 很老了,但也许有人还有这个问题,首先你需要安装插件 basic-branch-build-strategies:

如果您使用的是 jenkins dsl:

buildStrategies {
  buildAllBranches {
    strategies {
      skipInitialBuildOnFirstBranchIndexing()
    }
  }
}

在 Jenkins 待办事项列表中存在对此类功能的功能请求:JENKINS-63673 Allow configuring in NoTriggerBranchProperty which builds should be suppressed. I created a pull request today, so there is a chance it will be a part of the Branch API plugin in the future. In the meantime you may to use the custom version (see the build)。

如何使用JobDSL插件自动配置:

multibranchPipelineJob {
    // ...

    branchSources {
        branchSource {
            source {
               // ...
            }
            strategy {
               allBranchesSame {
                    props {
                        suppressAutomaticTriggering  {
                            strategyId(2)
                        }
                    }
                }
            }
        }
    }

    // ...
}

我们可以相应地修改branch-api-plugin。 https://github.com/jenkinsci/branch-api-plugin

此处src/main/java/jenkins/branch/OverrideIndexTriggersJobProperty.java 在这个文件中,它决定触发哪个任务构建

这里我修改了功能,不触发功能分支,但仍会添加到列表中。

默认情况下它计算复选框 Suppress Automatic SCM trigger

 @Extension
    public static class Dispatcher extends Queue.QueueDecisionHandler {

        private static final Logger LOGGER = Logger.getLogger(Dispatcher.class.getName());

        @SuppressWarnings("rawtypes") // untypable
        @Override
        public boolean shouldSchedule(Queue.Task p, List<Action> actions) {

                LOGGER.log(Level.INFO, "[TARUN_DEBUG] TASK NAME : "+ p.getName());
            if(p.getName().startsWith("feature")||p.getName().startsWith("bugfix")){
                return false;
            }
            else if(p.getName().startsWith("release")||p.getName().equals("master")||p.getName().startsWith("develop")||p.getName().startsWith("part of")||p.getName().startsWith("PR-")){
                
            }
            else{
                LOGGER.log(Level.INFO, "[TARUN_DEBUG] NOT TRIGGERED "+p.getName());
                return false;
            }

            for (Action action : actions) {
                if (action instanceof CauseAction) {
                    for (Cause c : ((CauseAction) action).getCauses()) {
                        if (c instanceof BranchIndexingCause) {
                            if (p instanceof Job) {
                                Job<?,?> j = (Job) p;
                                OverrideIndexTriggersJobProperty overrideProp = j.getProperty(OverrideIndexTriggersJobProperty.class);
                                if (overrideProp != null) {
                                    return overrideProp.getEnableTriggers();
                                } else {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            return true;
        }

要跳过由构建索引触发的构建,您可以将以下代码片段放入管道中。不需要额外的库。

when {
    // Run pipeline/stage only if not triggered by branch indexing.
    not {
        triggeredBy 'BranchIndexingCause'
    }
}