Search code examples
jenkinsjenkins-job-dslmultibranch-pipeline

multibranchPipelineJob job DSL: how to enable Discover branches and Suppress automatic SCM triggering


How to enable in a Jenkins job DSL for a multibranch pipeline the behavior Discover Benches and the Property strategy Suppress automatic SCM triggering?

enter image description here


Solution

  • It can be done like this:

    multibranchPipelineJob('job name') {
        branchSources {
            branchSource {
                source {
                    git {
                        remote('https://<repo address>.git')
                        credentialsId('credential id')
                    }
                }
                strategy {
                    defaultBranchPropertyStrategy {
                        props {
                            noTriggerBranchProperty()
                        }
                    }
                }
            }
        }
        configure {
            def traits = it / sources / data / 'jenkins.branch.BranchSource' / source / traits
            traits << 'jenkins.plugins.git.traits.BranchDiscoveryTrait' {}
        }
        triggers {
            periodic(2) // Trigger every 2 min.
        }
        orphanedItemStrategy { discardOldItems { numToKeep(-1) } }
    }