如何为 Job DSL 创建的多分支作业设置发现分支策略

时间:2021-06-07 16:54:23

标签: jenkins jenkins-pipeline jenkins-groovy jenkins-job-dsl

我正在通过 Groovy 创建一个多分支管道作业。

multibranchPipelineJob('example') {
    branchSources {
        github {
            id('23232323')
            scanCredentialsId('github-ci')
            repoOwner('OwnerName')
            repository('job-dsl-plugin')
        }
    }
    orphanedItemStrategy {
        discardOldItems {
            numToKeep(10)
        }
    }
}

这工作正常,但将发现分支策略设置为所有分支 enter image description here

有没有办法将排除也作为 PR 提交的分支设置为默认值? enter image description here

1 个答案:

答案 0 :(得分:1)

branchSources/github 是静态 API,您不应使用它。 Job DSL 插件的作者停止支持它。更安全的选择是使用动态 API。您可以使用以下 URL 检查 Jenkins 上可用的选项:

https://<your-jenkins>/plugin/job-dsl/api-viewer/index.html

这是你应该使用的:

multibranchPipelineJob('example') {
    branchSources {
        source {
            github {
                id('23232323')
                apiUri('apiUrl, example: https://github.com/api/v3')
                credentialsId('github-ci')
                repoOwner('OwnerName')
                repository('job-dsl-plugin')
                repositoryUrl('repositoryUrl')
                configuredByUrl(false)
                traits {
                    gitHubBranchDiscovery {
                        strategyId(1)
                    }
                }
            }    
        }
    }
    orphanedItemStrategy {
        discardOldItems {
            numToKeep(10)
        }
    }
}

策略 ID:

  • 1 - 发现所有分支,除了拉取请求源的分支
  • 2 - 仅发现拉取请求源的分支
  • 3 - 发现所有分支