Last active
September 10, 2019 14:04
-
-
Save steven-terrana/7d3b7bfd821b3e840a14f90cbbc33807 to your computer and use it in GitHub Desktop.
[Jenkins Seed Multibranch Jobs] initialize multibranch projects with the first sha #Jenkins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.boozallen.plugins.jte.config.*; | |
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject; | |
import org.jenkinsci.plugins.github_branch_source.GitHubSCMSource; | |
import hudson.plugins.git.GitSCM; | |
import hudson.plugins.git.BranchSpec; | |
import hudson.plugins.git.UserRemoteConfig; | |
import jenkins.branch.BranchSource; | |
import org.jenkinsci.plugins.github_branch_source.BranchDiscoveryTrait; | |
import org.jenkinsci.plugins.github_branch_source.OriginPullRequestDiscoveryTrait; | |
import jenkins.scm.api.trait.SCMSourceTrait; | |
import jenkins.scm.impl.trait.WildcardSCMHeadFilterTrait; | |
import org.boozallen.plugins.jte.config.TemplateConfigFolderProperty; | |
import org.boozallen.plugins.jte.config.GovernanceTier; | |
import org.boozallen.plugins.jte.job.TemplateBranchProjectFactory; | |
import jenkins.model.Jenkins; | |
import com.cloudbees.hudson.plugins.folder.*; | |
import jenkins.branch.buildstrategies.basic.SkipInitialBuildOnFirstBranchIndexing; | |
import jenkins.branch.BranchBuildStrategy; | |
import com.cloudbees.hudson.plugins.folder.computed.PeriodicFolderTrigger; | |
String githubCredId = "github" | |
String githubOwner = "myGitHubOwner" | |
Stirng repo = "myGitHubRepositoryName" | |
/* create GitHub SCM source */ | |
def scm_source = new GitHubSCMSource(owner, repo) | |
scm_source.setCredentialsId(githubCredId) | |
/* set configuration */ | |
List<SCMSourceTrait> traits = new ArrayList<>() | |
traits.add(new BranchDiscoveryTrait(1)) | |
traits.add(new OriginPullRequestDiscoveryTrait(1)) | |
scm_source.setTraits(traits) | |
/* create branch source from SCM */ | |
def branch_source = new BranchSource(scm_source) | |
/* use basic-build-strategy plugin to skip triggering the jobs on first index */ | |
List<BranchBuildStrategy> strats = [ new SkipInitialBuildOnFirstBranchIndexing() ] | |
branch_source.setBuildStrategies(strats) | |
List<BranchSource> sources = new ArrayList<>() | |
sources.add(branch_source) | |
/* create multibranch job */ | |
def multi_branch_job = new WorkflowMultiBranchProject(jenkins, job_name) | |
multi_branch_job.setSourcesList(sources) | |
/* sets the Build Recognizer to Jenkins Templating Engine */ | |
multi_branch_job.setProjectFactory(new TemplateBranchProjectFactory()) | |
/* have the job poll the SCM every minute */ | |
PeriodicFolderTrigger trigger = new PeriodicFolderTrigger("1") | |
multi_branch_job.addTrigger(trigger) | |
/* add job to jenkins */ | |
jenkins.add(multi_branch_job, multi_branch_job.name) | |
/* | |
the good part: | |
1. trigger first indexing and wait for it to complete. | |
*/ | |
def future = multi_branch_job.scheduleBuild2(0).getFuture() | |
while(!future.isDone()){ | |
sleep(1000) | |
} | |
/* | |
2. get the revision found during the first indexing | |
and set this to be the branch's current SHA so that | |
future indexing doesn't trigger unless there's been | |
a change | |
*/ | |
def factory = multi_branch_job.getProjectFactory() | |
multi_branch_job.getItems().findAll{ branch -> | |
def revision = factory.getLastSeenRevision(branch) | |
factory.setRevisionHash(branch, revision) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment