Skip to content

Commit

Permalink
feat!: Add seed job
Browse files Browse the repository at this point in the history
  • Loading branch information
turboBasic committed Dec 29, 2023
1 parent 592c99c commit 35c0943
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
95 changes: 95 additions & 0 deletions jobs/Admin/seed/Jenkinsfile
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'
}
29 changes: 29 additions & 0 deletions jobs/Admin/seed/Jenkinsfile.dsl
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"
}

0 comments on commit 35c0943

Please sign in to comment.