-
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.
Showing
3 changed files
with
151 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 🔹' | ||
} | ||
""".trim().replaceAll(/\s{2,}/, ' ')} | ||
|
||
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" | ||
} |
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,27 @@ | ||
package scripts.Admin | ||
|
||
import testSupport.PipelineSpockTestBase | ||
|
||
|
||
class seedSpec extends PipelineSpockTestBase { | ||
|
||
def 'Jenkinsfile works as expected'() { | ||
given: | ||
helper.registerAllowedMethod("findFiles", [Map], { Map args -> | ||
[ | ||
[path: 'jobs/Jenkinsfile.dsl'], | ||
[path: 'jobs/Foo/foo-1/Jenkinsfile.dsl'], | ||
[path: 'jobs/Foo/foo-2/Jenkinsfile.dsl'], | ||
[path: 'jobs/Bar/bar/Jenkinsfile.dsl'], | ||
[path: 'jobs/foo-bar/Jenkinsfile.dsl'], | ||
] | ||
}) | ||
helper.registerAllowedMethod("jobDsl", [Map]) | ||
when: | ||
runScript 'Admin/seed/Jenkinsfile' | ||
then: | ||
printCallStack() | ||
assertJobStatusSuccess() | ||
} | ||
|
||
} |