Roberto Leinardi
Roberto Leinardi

Reputation: 14389

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

Upvotes: 4

Views: 5218

Answers (2)

WindyFields
WindyFields

Reputation: 2875

Just to extend the accepted answer: you don't necessary need that tricky traits << ... syntax to add branch (or tags) discovery. Instead you may use traits { } section (see docs).

multibranchPipelineJob 'job name', {
    branchSources {
        branchSource {
            source {
                git {
                    id 'job name'
                    remote 'git@<repo-url>.git'
                    credentialsId 'git-creds-id'

                    traits {
                        gitBranchDiscovery()
                        gitTagDiscovery() // if you need tag discovery
                    }
                }
            }
            strategy {
                defaultBranchPropertyStrategy {
                    props {
                        noTriggerBranchProperty()
                    }
                }
            }
        }
    }
}

Upvotes: 4

Roberto Leinardi
Roberto Leinardi

Reputation: 14389

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

Upvotes: 7

Related Questions