|
| 1 | +#!groovy |
| 2 | +/* groovylint-disable GStringExpressionWithinString, JavaIoPackageAccess, NoDef, NoWildcardImports, UnnecessaryGetter, UnnecessaryObjectReferences, UnnecessarySetter, VariableTypeRequired */ |
| 3 | +import hudson.model.FreeStyleProject |
| 4 | +import hudson.triggers.SCMTrigger |
| 5 | +import javaposse.jobdsl.plugin.* |
| 6 | +import jenkins.model.Jenkins |
| 7 | +import hudson.plugins.filesystem_scm.FSSCM |
| 8 | + |
| 9 | +println('==== Creating seed job') |
| 10 | +final String jobName = 'seed-job' |
| 11 | +final File jobsDir = new File('/var/jenkins_home/pipeline-library/jobs') |
| 12 | + |
| 13 | +final String dslScript = '''\ |
| 14 | +import java.nio.file.Path |
| 15 | +import java.nio.file.Files |
| 16 | +import java.nio.file.Paths |
| 17 | +
|
| 18 | +
|
| 19 | +Files.walk(Paths.get('/var/jenkins_home/pipeline-library/jobs')).findAll { Path item -> |
| 20 | + item.toString().contains('.groovy') |
| 21 | +}.forEach { Path item -> |
| 22 | + final String fileName = item.getFileName() |
| 23 | + final String jobName = fileName.replace('_', '-').replace('.groovy', '') |
| 24 | + final File jenkinsFile = item.toFile() |
| 25 | + println("Creating job for ${fileName}") |
| 26 | +
|
| 27 | + pipelineJob(jobName) { |
| 28 | +
|
| 29 | + definition { |
| 30 | + cpsScm { |
| 31 | +
|
| 32 | + lightweight(true) |
| 33 | +
|
| 34 | + scm { |
| 35 | + filesystem { |
| 36 | +
|
| 37 | + // The file path for the source code. |
| 38 | + path(jenkinsFile.getParent()) |
| 39 | + // If true, the system will delete all existing files/sub-folders in workspace before checking-out. |
| 40 | + clearWorkspace(false) |
| 41 | + // If true, the system will copy hidden files and folders as well. |
| 42 | + copyHidden(false) |
| 43 | + filterSettings { |
| 44 | + includeFilter(false) |
| 45 | + } |
| 46 | +
|
| 47 | + } |
| 48 | + } |
| 49 | +
|
| 50 | + scriptPath(fileName) |
| 51 | +
|
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | +''' |
| 57 | + |
| 58 | +final FSSCM fileScm = new FSSCM(jobsDir.getPath(), false, false, false, false, null) |
| 59 | + |
| 60 | +final Jenkins jenkins = Jenkins.getInstance() |
| 61 | + |
| 62 | +def existingJob = jenkins.items.find { def job -> |
| 63 | + job.name == jobName |
| 64 | +} |
| 65 | + |
| 66 | +if (existingJob) { |
| 67 | + println('== Existing seed job found, exiting') |
| 68 | + return |
| 69 | +} |
| 70 | + |
| 71 | +final SCMTrigger scmTrigger = new SCMTrigger('* * * * *') |
| 72 | +final ExecuteDslScripts dslBuilder = new ExecuteDslScripts() |
| 73 | + |
| 74 | +// dslBuilder.setSandbox(true) |
| 75 | +dslBuilder.setScriptText(dslScript) |
| 76 | +dslBuilder.setUseScriptText(true) |
| 77 | +dslBuilder.setIgnoreExisting(false) |
| 78 | +dslBuilder.setIgnoreMissingFiles(false) |
| 79 | +dslBuilder.setFailOnMissingPlugin(true) |
| 80 | +dslBuilder.setRemovedJobAction(RemovedJobAction.DELETE) |
| 81 | +dslBuilder.setRemovedViewAction(RemovedViewAction.DELETE) |
| 82 | +dslBuilder.setLookupStrategy(LookupStrategy.JENKINS_ROOT) |
| 83 | + |
| 84 | +dslProject = new FreeStyleProject(jenkins, jobName) |
| 85 | +dslProject.scm = fileScm |
| 86 | + |
| 87 | + |
| 88 | +dslProject.addTrigger(scmTrigger) |
| 89 | +dslProject.createTransientActions() |
| 90 | +dslProject.getPublishersList().add(dslBuilder) |
| 91 | + |
| 92 | +println('== Adding Seed Job to Jenkins') |
| 93 | +jenkins.add(dslProject, jobName) |
| 94 | + |
| 95 | +println('== Triggering Seed Job polling') |
| 96 | +scmTrigger.start(dslProject, true) |
| 97 | + |
| 98 | +println('== Seed Job setup complete') |
0 commit comments