Jenkins — DSL Multibranch Pipeline создание Seed Job’ом

В репозитории, где будут храниться будущие groovy файлы создадим директорию с именем «utilities» и в нем создадим файл «BitbucketMultibranch.groovy» со следующим содержимым:

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

Теперь для создания Item‘а достаточно создать в корне репозитория файл с расширением groovy, для того, чтобы на 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)

 

Сборка для 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)

Метки: Метки

Подписаться
Уведомить о
guest

0 комментариев
Межтекстовые Отзывы
Посмотреть все комментарии