I am having trouble configuring my job dsl for a github organization folder. I have tried to automate the discovery for the branches based on opened pull requests. I have tried to use the deprecated: buildOriginBranchWithPR(true)
without any success. I also tried my hand with: gitBranchDiscovery()
and gitTagDiscovery()
without much results either.
I know that the changes have several issues opened and that a few workaround have been proposed. One of them is to use the configure block from the job dsl to directly interact with the xml. I haven't managed to use it properly.
I have managed to make it work if I configure it with the GUI directly inside Jenkins but I would like to avoid that if possible.
Please see here an example of the job I am trying to define:
organizationFolder('example-org') {
description('This contains branch source jobs for example-org GitHub')
displayName('example-org')
triggers {
periodic(2400)
}
organizations {
github {
repoOwner("example-owner")
apiUri("https://api.github.com")
credentialsId('jenkins-token')
traits {
publicRepoPullRequestFilterTrait()
}
}
}
configure {
def traits = it / sources / data / 'jenkins.branch.BranchSource' / source / traits
traits << 'org.jenkinsci.plugins.github__branch__source.BranchDiscoveryTrait' {
strategyId(2)
}
traits << 'org.jenkinsci.plugins.github__branch__source.OriginPullRequestDiscoveryTrait' {
strategyId(2)
}
}
projectFactories {
workflowMultiBranchProjectFactory {
// Relative location within the checkout of your Pipeline script.
scriptPath("Jenkinsfile")
}
}
}
I am using the last version from the official jenkins docker image.
Thanks in advance for your help.
The config.xml generated for the organization folder job is different than that of the multibranch pipeline job. You need to modify the configure block, to something similar to this (notice the path to get the traits list changes):
configure {
def traits = it / navigators / 'org.jenkinsci.plugins.github__branch__source.GitHubSCMNavigator' / traits
traits << 'org.jenkinsci.plugins.github_branch_source.BranchDiscoveryTrait' {
strategyId 1
}
traits << 'org.jenkinsci.plugins.github_branch_source.ForkPullRequestDiscoveryTrait' {
strategyId 2
trust(class: 'org.jenkinsci.plugins.github_branch_source.ForkPullRequestDiscoveryTrait$TrustEveryone')
}
traits << 'org.jenkinsci.plugins.github__branch__source.OriginPullRequestDiscoveryTrait' {
strategyId 2
}
}