Skip to content

Commit

Permalink
feat(jenkins)!: 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 5ac0f3d
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
85 changes: 85 additions & 0 deletions jobs/Admin/seed/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
withNode {
stage('checkout') {
//checkout scm
git branch: 'main', url: 'https://github.com/turboBasic/JenkinsLibrary'
}
stage('seed') {
getJobFolders().each {
seedFolder it
}
getDslFiles().each {
seedJob it
}
}
}

void withNode(Closure body) {
Closure wrappedBody = { -> ansiColor 'xterm', body }
if (env.NODE_LABEL) {
node env.NODE_LABEL, wrappedBody
}
else {
node wrappedBody
}
}

List<String> getDslFiles() {
findFiles(glob: 'jobs/**/Jenkinsfile.dsl')
.findAll { !it.directory }
.collect { it.path }
.toSorted()
}

List<String> getJobFolders() {
getDslFiles()
.collect {
it.replaceFirst('^jobs/', '')
.replaceFirst('([^/]+/)?Jenkinsfile[.]dsl$', '')
.replaceFirst('/$', '')
}
.findAll { it != '' }
.toSorted()
.toUnique()
}

void seedJob(String dslFileName) {
final String LOOKUP_STRATEGY = 'SEED_JOB' // 'JENKINS_ROOT'

catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
jobDsl targets: dslFileName,
lookupStrategy: LOOKUP_STRATEGY,
ignoreExisting: false,
ignoreMissingFiles: true,
removedJobAction: 'DISABLE',
removedConfigFilesAction: 'IGNORE',
removedViewAction: 'DELETE',
unstableOnDeprecation: true,
//additionalClasspath: 'src/main/groovy',
additionalParameters: [
WORKSPACE: env.WORKSPACE,
DSL_ROOT : env.WORKSPACE,
]
}
}

void seedFolder(String folder) {
final String LOOKUP_STRATEGY = 'SEED_JOB' // 'JENKINS_ROOT'

catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
jobDsl lookupStrategy: LOOKUP_STRATEGY,
removedJobAction: 'DISABLE',
removedConfigFilesAction: 'DELETE',
removedViewAction: 'DELETE',
scriptText: folderDslScript(folder)
}
}

String folderDslScript(String folder) {"""
folder '$folder', {
displayName '$folder 🔹'
}
"""}

String getJobsPrefix() {
'jobs/'
}
14 changes: 14 additions & 0 deletions jobs/Admin/seed/Jenkinsfile.dsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pipelineJob 'Admin/seed', {
definition {
cpsScm {
scm {
github 'turboBasic/JenkinsLibrary', 'main'
}
scriptPath jenkinsFileName('Admin/seed')
}
}
}

String jenkinsFileName(String jobName) {
"jobs/$jobName/Jenkinsfile"
}

0 comments on commit 5ac0f3d

Please sign in to comment.