-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
592c99c
commit 35c0943
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import javaposse.jobdsl.plugin.LookupStrategy | ||
|
||
|
||
withNode { | ||
stage('checkout') { | ||
checkout scm | ||
} | ||
stage('seed') { | ||
getJobFolders().each { | ||
seedFolder it | ||
} | ||
getDslFiles().each { | ||
seedJob it | ||
} | ||
} | ||
} | ||
|
||
void withNode(Closure body) { | ||
node getNodeName(), { | ||
ansiColor 'xterm', body | ||
} | ||
} | ||
|
||
List<String> getDslFiles() { | ||
findFiles(glob: "${getJobsPrefix()}**/Jenkinsfile.dsl") | ||
.findAll { !it.directory } | ||
.collect { it.path } | ||
.toSorted() | ||
} | ||
|
||
List<String> getJobFolders() { | ||
getDslFiles() | ||
.collect { | ||
it.replaceFirst("^${getJobsPrefix()}", '') | ||
.replaceFirst('([^/]+/)?Jenkinsfile[.]dsl$', '') | ||
.replaceFirst('/$', '') | ||
} | ||
.findAll { it != '' } | ||
.toSorted() | ||
.toUnique() | ||
} | ||
|
||
void seedJob(String dslFileName) { | ||
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') { | ||
jobDsl targets: dslFileName, | ||
lookupStrategy: getLookupStrategy(), | ||
ignoreExisting: false, | ||
ignoreMissingFiles: true, | ||
removedJobAction: 'DISABLE', | ||
removedConfigFilesAction: 'IGNORE', | ||
removedViewAction: 'DELETE', | ||
unstableOnDeprecation: true, | ||
additionalClasspath: 'src', | ||
additionalParameters: [ | ||
WORKSPACE: env.WORKSPACE, | ||
DSL_ROOT : env.WORKSPACE, | ||
] | ||
} | ||
} | ||
|
||
void seedFolder(String folder) { | ||
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') { | ||
jobDsl scriptText: folderDslScript(folder), | ||
lookupStrategy: getLookupStrategy(), | ||
removedJobAction: 'DISABLE', | ||
removedConfigFilesAction: 'DELETE', | ||
removedViewAction: 'DELETE', | ||
additionalClasspath: 'src', | ||
additionalParameters: [ | ||
WORKSPACE: env.WORKSPACE, | ||
DSL_ROOT : env.WORKSPACE, | ||
] | ||
} | ||
} | ||
|
||
String folderDslScript(String folder) {""" | ||
folder '$folder', { | ||
displayName '$folder 🔹' | ||
} | ||
"""} | ||
|
||
String getJobsPrefix() { | ||
'jobs/' | ||
} | ||
|
||
String getLookupStrategy() { | ||
if (LookupStrategy.values().any { params.LOOKUP_STRATEGY == it.name() }) { | ||
return params.LOOKUP_STRATEGY | ||
} | ||
'SEED_JOB' | ||
} | ||
|
||
String getNodeName() { | ||
env.NODE_LABEL ?: 'admin' | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
pipelineJob 'Admin/seed', { | ||
parameters { | ||
choiceParam { | ||
name 'LOOKUP_STRATEGY' | ||
description 'Describes how relative path to jobs are processed: relative to the Seed job or Jenkins root' | ||
choices(['SEED_JOB', 'JENKINS_ROOT']) | ||
} | ||
} | ||
properties { | ||
disableConcurrentBuilds() | ||
} | ||
definition { | ||
cpsScm { | ||
scm { | ||
git { | ||
remote { | ||
url 'https://github.com/turboBasic/JenkinsLibrary' | ||
} | ||
branch 'main' | ||
} | ||
} | ||
scriptPath jenkinsFileName('Admin/seed') | ||
} | ||
} | ||
} | ||
|
||
String jenkinsFileName(String jobName) { | ||
"jobs/$jobName/Jenkinsfile" | ||
} |