Issue
I'm new to Jenkins and I'm trying to figure something out.
Is there a way to add Branch Sources behavior via Groovy. This is to analyse GitHub projects in SonarQube using Jenkins.
I'm creating a multi-branch pipeline but can't seem to figure out how to add the following behaviours.
These behaviours come by default when the job is created in the UI, but don't appear when the job is created via Groovy.
I've defined this as my pipeline. Any idea how these other parameters can be added in?
multibranchPipelineJob('Foo') {
displayName('Foo')
description('Jenkins')
branchSources {
branchSource {
source {
github {
id('15')
repoOwner('12345')
repository('foo')
repositoryUrl('https://example.com')
configuredByUrl(true)
credentialsId('foo')
traits {
gitBranchDiscovery()
}
}
}
}
}
orphanedItemStrategy {
discardOldItems {
numToKeep(10)
}
}
}
I've tried adding in the following parameters but it throws an error.
import jenkins.plugins.git.traits.*
def traits = []
traits.add(new TagDiscoveryTrait())
traits.add(new LocalBranchTrait())
gitSCMSource.setTraits(traits)
Is there a way to create the job via Groovy but with the default settings that would appear when the job is created in the UI?
Solution
You can check all available options on your Jenkins by using this URL:
https://<your-jenkins>/plugin/job-dsl/api-viewer/index.html
Branch Discovery Mode
multibranchPipelineJob('Foo') {
branchSources {
branchSource {
source {
github {
traits {
gitHubPullRequestDiscovery {
strategyId(1)
// strategyId(2)
// strategyId(3)
}
}
}
}
}
}
}
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
Pull Request Discovery Mode
multibranchPipelineJob('Foo') {
branchSources {
branchSource {
source {
github {
traits {
gitHubPullRequestDiscovery {
strategyId(1)
// strategyId(2)
// strategyId(3)
}
}
}
}
}
}
}
Strategy id:
- 1 - discover each pull request once with the discovered revision corresponding to the result of merging with the current revision of the target branch
- 2 - discover each pull request once with the discovered revision corresponding to the pull request head revision without merging
- 3 - discover the current pull request revision and the pull request merged with the current target branch revision
Fork Pull Request Discovery Mode
Due to a bug in Jenkins (JENKINS-60874), you have to use the configure
block:
multibranchPipelineJob('Foo') {
configure {
def traits = it / 'sources' / 'data' / 'jenkins.branch.BranchSource' / 'source' / 'traits'
traits << 'org.jenkinsci.plugins.github__branch__source.ForkPullRequestDiscoveryTrait' {
strategyId(1)
// strategyId(2)
// strategyId(3)
trust(class: 'org.jenkinsci.plugins.github_branch_source.ForkPullRequestDiscoveryTrait$TrustPermission')
// trust(class: 'org.jenkinsci.plugins.github_branch_source.ForkPullRequestDiscoveryTrait$TrustNobody')
// trust(class: 'org.jenkinsci.plugins.github_branch_source.ForkPullRequestDiscoveryTrait$TrustContributors')
// trust(class: 'org.jenkinsci.plugins.github_branch_source.ForkPullRequestDiscoveryTrait$TrustEveryone')
}
}
}
Strategy id:
- 1 - discover each pull request once with the discovered revision corresponding to the result of merging with the current revision of the target branch
- 2 - discover each pull request once with the discovered revision corresponding to the pull request head revision without merging
- 3 - discover the current pull request revision and the pull request merged with the current target branch revision
Trust class:
- TrustPermission - trust those with write permission to the repository
- TrustNobody - trust nobody
- TrustContributors - trust contributors to the repository
- TrustEveryone - trust everyone
Answered By - agabrys