I needed a little groovy code snippet that generates a MultiBranch Pipeline Project in a Jenkins 2.* Script Console. Didn't find it on the InterWeb, so I made it myself. Eventually I got it to work by glueing different code pieces together. I wonder if a groovy guru could help me optimize it and make it more beautiful? :-)
My further plan: Would be nice if I didn't have to manually enter the git repo. A scan over all repos in a git organisation would be nice.
import jenkins.model.Jenkins
import jenkins.plugins.git.*
import hudson.triggers.*
import com.cloudbees.hudson.plugins.folder.Folder
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject
import jenkins.branch.BranchProperty
import jenkins.branch.BranchSource
import jenkins.branch.DefaultBranchPropertyStrategy
import org.jenkinsci.plugins.workflow.libs.*
def parent = Jenkins.instance
println "parent: $parent"
// create folder
Folder folder = Jenkins.instance.getItemByFullName('poc')
if (folder == null) {
folder = parent.createProject(Folder.class, "poc")
folder.displayName = "P.O.C Jobs"
folder.getProperties().add(
new FolderLibraries(
Collections.singletonList(
new LibraryConfiguration(
"pipelinejunit",
new SCMSourceRetriever(
new GitSCMSource(
null,
"ssh://git@servix/extra/git/poc_junit.git",
"local_git",
"*",
"",
true)
)
)
)
)
)
}
println "folder: $folder"
WorkflowMultiBranchProject mbp = Jenkins.instance.getItemByFullName('p_junit-lib_mb')
if (mbp != null) {
mbp.delete()
}
mbp = folder.createProject(WorkflowMultiBranchProject.class, "p_junit-lib_mb")
mbp.displayName = "Junit Library [build/test *]"
mbp.getSourcesList().add(new BranchSource(new GitSCMSource(null, "ssh://git@servix/extra/git/poc_junit.git", "local_git", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
mbp.scheduleBuild2(0).getFuture().get()
println "mbp: $mbp"
def job
while (/* do */ {
Thread.sleep(10)
job = mbp.getItem('master')
(job == null)
}());
println "job master: $job"
job.displayName = "Branch: master"
job = mbp.getItem('development')
println "job development: $job"
job.displayName = "Branch: development"
// reload jobs
parent.reload()
My further plan: Would be nice if I didn't have to manually enter the git repo. A scan over all repos in a git organisation would be nice.
I think what you want is already implemented by the very popular Github Branch Source Plugin . From the docs : "The GitHub Branch Source Plugin allows you to create a new project based on the repository structure from one or more GitHub users or organizations. You can either: 1) Import all or a subset of repositories as jobs into the workspace from a GitHub user or organization or 2) Import a single repository's branches as jobs from a GitHub user or organization." If you're not using either github or bitbucket, you wouldn't be able to use these awesome plugins, but hopefully you are. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.