詹金斯多分支管道扫描而不执行



是否可以扫描多分支管道以检测具有Jenkinsfile的分支,但不执行管道?

我的项目有不同的分支,我不希望当我从父管道多分支启动构建扫描时,所有带有 Jenkinsfile 的子管道分支都开始执行。

"分支源"部分中,可以添加名为"抑制自动 SCM 触发"的属性。

这可以防止 Jenkins 使用 Jenkinsfile 构建所有内容。

此外,您可以通过编程方式执行此操作

import jenkins.branch.*
import jenkins.model.Jenkins

for (f in Jenkins.instance.getAllItems(jenkins.branch.MultiBranchProject.class)) {
  if (f.parent instanceof jenkins.branch.OrganizationFolder) {
    continue;
  }
  for (s in f.sources) {
    def prop = new jenkins.branch.NoTriggerBranchProperty();
    def propList = [prop] as jenkins.branch.BranchProperty[];
    def strategy = new jenkins.branch.DefaultBranchPropertyStrategy(propList);
    s.setStrategy(strategy);
  }
  f.computation.run()
}

这是一个可以在 Jenkins 中执行的 Groovy 片段,它将进行扫描,但不会为所有发现的分支启动新的"构建"。

如果您使用的是job-dsl,您可以简单地执行此操作,它将扫描所有内容,而无需在第一次索引时实际运行构建。

organizationFolder('Some folder name') {
  buildStrategies {
    skipInitialBuildOnFirstBranchIndexing()
  }
}

要添加到@Stqs的答案中,您还可以使用Job DSL插件设置noTriggerBranchProperty它,例如:

multibranchPipelineJob('example') {
  ...
  branchSources {
    branchSource {
      ...
      strategy {
        defaultBranchPropertyStrategy {
          props {
            // Suppresses the normal SCM commit trigger coming from branch indexing
            noTriggerBranchProperty()
            ...
          }
        }
      }
    }
  }
  ...
}
organizationFolder('my-folder') {
  buildStrategies {
     buildRegularBranches()
     buildChangeRequests {
       ignoreTargetOnlyChanges true
       ignoreUntrustedChanges false
     }
   }
}

注意:需要插件basic-branch-build-strategies

引用:

  • https://issues.jenkins.io/browse/JENKINS-63799
  • http://jenkins-ci.361315.n4.nabble.com/JobDSL-an-example-of-configuring-a-bitbucket-source-trait-of-bitbucketForkDiscovery-in-the-multibrand-td5014968.html#a5015085

经过一番挣扎,我找到了这个解决方案,它应该只避免在分支索引时触发构建,而不是在提交后禁用自动构建。只需在项目的第一阶段添加它:

when {
    not {
        expression {
            def causes = currentBuild.getBuildCauses()
            String causesClass = causes._class[0]
            return causesClass.contains('BranchIndexingCause')
        }
    }
}

最新更新