1

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

agabrys
  • 8,728
  • 3
  • 35
  • 73
Hammed
  • 1,457
  • 1
  • 24
  • 37
  • Check https://docs.cloudbees.com/docs/admin-resources/latest/multibranch-pipeline-template-syntax-guide/github – MaratC Jun 07 '21 at 19:56
  • I saw this but it seems to assume that the configuration file is in YAML. Mine's Groovy and for some reason, Jenkins doesn't seem to like traits with the most recent version of the DSL plugin. Or maybe I'm doing something wrong. – Hammed Jun 08 '21 at 08:09
  • 1
    I found that somebody asked a question which cover more discovery modes: https://stackoverflow.com/q/67871598/4944847 – agabrys Jun 08 '21 at 12:35

1 Answers1

2

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
agabrys
  • 8,728
  • 3
  • 35
  • 73
  • Thank you @agabrys. This makes sense. Out of curiosity, is there a way to get this line ```repository('job-dsl-plugin')``` to scan all repositories as opposed to just one? – Hammed Jun 08 '21 at 16:41
  • 1
    No. Multibranch job is associated with a specific repository. It should be possible by using GitHub Organization Folder which creates for every repository a new job. I've never used it, so I cannot tell you how to do it. – agabrys Jun 09 '21 at 18:15
  • Thank you @agabrys. – Hammed Jun 09 '21 at 18:54