Jenkins – DSL Multibranch Pipeline Creating by Seed Job

In the repository where the future groovy files will be stored, create a directory called “utilities” and create the file “BitbucketMultibranch.groovy” in it with the following contents:

package utilities

import javaposse.jobdsl.dsl.DslFactory

class BitbucketMultibranch {

    String name
    String description
    String displayName
    String serverUrl
    String repoOwner
    String repository
    String credentialsId
    String includeBranches
    String excludeBranches

	void build(DslFactory dslFactory) {
	    def job = dslFactory.multibranchPipelineJob(name) {
	        description(description)
	        displayName(displayName)
	        branchSources {
	            branchSource {
	                source {
	                    bitbucket {
	                        serverUrl(serverUrl)
	                        repoOwner(repoOwner)
	                        repository(repository)
	                        credentialsId(credentialsId)
	                        traits {
	                            headWildcardFilter {
	                                excludes(excludeBranches)
	                                includes(includeBranches)
	                            }
	                        }
	                    }
	                }
	            }
	        }
	        configure {
	          	def traits = it / sources / data / 'jenkins.branch.BranchSource' / source / traits
	          	traits << 'com.cloudbees.jenkins.plugins.bitbucket.BranchDiscoveryTrait' {
	            	strategyId(3) // detect all branches
	          	}
	        }
        	factory {
	            workflowBranchProjectFactory {
	                scriptPath('.jenkins/Jenkinsfile')
	            }
	        }
	        orphanedItemStrategy {
	            discardOldItems {
	                numToKeep(15)
	            }
	        }
	    }
	}
}

Now, to create an Item, it is enough to create a file with the groovy extension in the root of the repository, so that it can be processed by Seed Job.

artem-dsl.groovy

import utilities.BitbucketMultibranch

def multiPipeline = new BitbucketMultibranch(
    description: 'Just try make world better',
    name: 'Artem-DSL',
    displayName: 'Artem-DSL',
    serverUrl: 'https://git.artem.services',
    repoOwner: "dev",
    repository: "artem-dls",
    credentialsId: 'svc-bitbucket',
    includeBranches: 'development staging master',
    excludeBranches: ''
).build(this)

 

Build for GitHub.

GitHubMultibranch.groovy:

package utilities

import javaposse.jobdsl.dsl.DslFactory

class GithubMultibranch {

    String name
    String description
    String displayName
    String repositoryOwner
    String repositoryName
    String credentials
    String includeBranches
    String excludeBranches


	void build(DslFactory dslFactory) {
	    def job = dslFactory.multibranchPipelineJob(name) {
	        description(description)
	        displayName(displayName)
		    branchSources {
		        github {
		            scanCredentialsId(credentials)
		            repoOwner(repositoryOwner)
		            repository(repositoryName)
		            includes(includeBranches)
		            excludes(excludeBranches)
		        }
		    }
		    factory {
		        workflowBranchProjectFactory {
		            scriptPath('.jenkins/Jenkinsfile')
		        }
		    }
		    orphanedItemStrategy {
		        discardOldItems {
		            numToKeep(15)
		        }
		    }
		}
	}
}

artem-github.groovy:

import utilities.GithubMultibranch


def multiPipeline = new GithubMultibranch(
    description: 'Just try make world better',
    name: 'Github-Test',
    displayName: 'Github-Test',
    repositoryOwner: "artem",
    repositoryName: "dsl-test",
    credentials: 'artem-github',
    includeBranches: 'development staging master',
    excludeBranches: ''
).build(this)
0 0 vote
Article Rating

Tagged: Tags

Подписаться
Уведомление о
guest
2 Comments
старее
новее большинство голосов
Inline Feedbacks
View all comments
Akshay
Akshay
1 год назад

Hi,
But anyhow, I have to create a seed job.
I want a groovy script/python which should create a Multi branch pipeline job automatically without creating even a seed job.
What do you think , is it possible?