Hammed
Hammed

Reputation: 1557

How to set the discover branches strategy for multibranch job created by Job DSL

I'm creating a multi-branch pipeline job via Groovy.

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

This works fine, but sets the Discover Branches Strategy to All branches enter image description here

Is there a way to set Exclude Branches that are also filed as PRs to be the default? enter image description here

Upvotes: 1

Views: 4480

Answers (1)

agabrys
agabrys

Reputation: 9126

branchSources/github is a static API and you shouldn't use it. The author of the Job DSL plugin stopped supporting it. The safer option is to use a dynamic API. You can check which options are available on your Jenkins by using this URL:

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

This is what you should use:

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)
        }
    }
}

Strategy id:

  • 1 - discover all branches, except branches that are pull request sources
  • 2 - discover only branches that are pull request sources
  • 3 - discover all branches

Upvotes: 2

Related Questions