Skip to content

Instantly share code, notes, and snippets.

@Laxman-SM
Forked from ealebed/create-jobs.groovy
Created February 3, 2020 09:23
Show Gist options
  • Save Laxman-SM/fc8d62897afc28906aaaaf134bfac211 to your computer and use it in GitHub Desktop.
Save Laxman-SM/fc8d62897afc28906aaaaf134bfac211 to your computer and use it in GitHub Desktop.
Groovy script for creating Jenkins job from .yaml file
@Grab(group='org.yaml', module='snakeyaml', version='1.18')
import jenkins.model.*
import hudson.model.*
import hudson.tasks.LogRotator
import hudson.plugins.git.*
import hudson.plugins.git.extensions.*
import hudson.plugins.git.extensions.impl.*
import org.jenkinsci.plugins.workflow.job.WorkflowJob
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition
import org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty
import com.coravy.hudson.plugins.github.*
import com.cloudbees.jenkins.GitHubPushTrigger
import org.yaml.snakeyaml.Yaml
import org.jenkinsci.plugins.github.pullrequest.events.impl.*
import org.jenkinsci.plugins.github.pullrequest.*
Jenkins jenkins = Jenkins.get()
def pluginParameter = "workflow-aggregator github-pullrequest"
def plugins = pluginParameter.split()
def pm = jenkins.getPluginManager()
def installed = false
plugins.each {
if (!pm.getPlugin(it)) {
println("Plugin ${it} not installed, skip creating jobs!")
} else {
installed = true
}
}
if(installed) {
def listExistingJob = jenkins.items.collect { it.name }
def listExistingViews = jenkins.views.collect { it.name }
def jobParameters = new Yaml().load(new FileReader('/root/job_list.yaml'))
for (view in jobParameters) {
if (jobParameters.any { listExistingViews.contains(view.key) }) {
println("--- View ${view.key} already exist, skip")
} else {
println("--- Create new view ${view.key}")
jenkins.addView(new ListView(view.key))
}
for (item in view.value) {
def jobName = item.key
def config = view.value.get(item.key)
def jobDisabled = config['jobDisabled'] ?: false
def enableGithubPolling = config['enableGithubPolling'] ?: false
def enableGithubPRTrig = config['enableGithubPRTrig'] ?: false
def daysBuildsKeep = config['daysBuildsKeep'] ?: '7'
def numberBuildsKeep = config['numberBuildsKeep'] ?: '15'
def githubRepoUrl = config['githubRepoUrl']
def githubRepo = config['githubRepo']
def githubBranch = config['githubBranch']
def advancedBehaviors = config['advancedBehaviors'] ?: 'CleanBeforeCheckout'
def description = config['description'] ?: ""
def jenkinsfilePath = config['jenkinsfilePath'] ?: "Jenkinsfile"
def branchConfig = []
if (githubBranch) {
branchConfig = githubBranch.split('[,;]').collect { it.trim() }.findAll { it != "" }.collect { new BranchSpec(it) }
}
if (branchConfig.isEmpty()) {
branchConfig = [new BranchSpec("*/master")]
}
def m = [
"CleanCheckout": { new CleanCheckout() },
"CleanBeforeCheckout": { new CleanBeforeCheckout() },
"PruneStaleBranch": { new PruneStaleBranch() },
"WipeWorkspace": { new WipeWorkspace() }
]
def extensions = []
if (advancedBehaviors) {
extensions = advancedBehaviors.split("[;,]").collect { it.trim() }.toSet().collect { m["${it}"]?.call() }.findAll { it }
}
def nameRepo = ""
def refspec = ""
if ( enableGithubPRTrig ) {
nameRepo = 'origin'
refspec = '+refs/pull/${GITHUB_PR_NUMBER}/merge:refs/remotes/origin/pull/${GITHUB_PR_NUMBER}/merge'
}
def userConfig = [new UserRemoteConfig(githubRepo, nameRepo, refspec, null)]
def scm = new GitSCM(userConfig, branchConfig, false, [], null, null, extensions)
def flowDefinition = new CpsScmFlowDefinition(scm, jenkinsfilePath)
if (view.value.any { listExistingJob.contains(item.key) }) {
println("--- Job ${item.key} already exist, check params")
Jenkins.instance.views.each { v ->
if ( view.key == v.name ) {
v.items.each { j ->
if ( item.key == j.name ) {
if ( jenkinsfilePath != j.definition.scriptPath ||
advancedBehaviors != j.definition.getScm().getExtensions().toString().replace("[", "").replace("]", "").replace("{}", "") ||
githubBranch != j.definition.getScm().getBranches().name.toString().replace("[", "").replace("]", "") ||
githubRepo != j.definition.getScm().getUserRemoteConfigs().url.toString().replace("[", "").replace("]", "") ||
nameRepo != j.definition.getScm().getUserRemoteConfigs().name.toString().replace("[", "").replace("]", "") ||
refspec != j.definition.getScm().getUserRemoteConfigs().refspec.toString().replace("[", "").replace("]", "")
) {
j.definition = flowDefinition
}
if ( description != j.description ) {
j.setDescription(description)
}
if ( githubRepoUrl != j.getProperty(GithubProjectProperty.class).getProjectUrl().baseUrl() ) {
j.addProperty(new GithubProjectProperty(githubRepoUrl))
}
if ( numberBuildsKeep != j.buildDiscarder.numToKeep.toString() || daysBuildsKeep != j.buildDiscarder.daysToKeep.toString() ) {
j.addProperty(new BuildDiscarderProperty(new LogRotator(daysBuildsKeep, numberBuildsKeep, null, null)))
}
PipelineTriggersJobProperty triggersJobProperty = j.getProperty(PipelineTriggersJobProperty.class)
if ( triggersJobProperty ) {
j.removeProperty(triggersJobProperty)
}
if ( true == enableGithubPolling ) {
j.addTrigger(new GitHubPushTrigger())
}
if ( true == enableGithubPRTrig ) {
GitHubPRTrigger trigger = new GitHubPRTrigger("H/2 * * * *", GitHubPRTriggerMode.CRON, [new GitHubPROpenEvent()])
j.addTrigger(trigger)
}
if ( jobDisabled != j.disabled ) {
j.setDisabled(jobDisabled)
}
}
}
}
}
} else {
println("--- Create new job ${item.key}")
flowDefinition.setLightweight(true)
def job = new WorkflowJob(jenkins, jobName)
job.definition = flowDefinition
job.setDescription(description)
job.setConcurrentBuild(false)
job.setDisabled(jobDisabled)
job.addProperty(new BuildDiscarderProperty(new LogRotator(daysBuildsKeep, numberBuildsKeep, null, null)))
job.addProperty(new GithubProjectProperty(githubRepoUrl))
if (true == enableGithubPolling) {
job.addTrigger(new GitHubPushTrigger())
}
if ( true == enableGithubPRTrig ) {
GitHubPRTrigger trigger = new GitHubPRTrigger("H/2 * * * *", GitHubPRTriggerMode.CRON, [new GitHubPROpenEvent()])
job.addTrigger(trigger)
}
jenkins.save()
jenkins.reload()
hudson.model.Hudson.instance.getView(view.key).doAddJobToView(jobName)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment