Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add seed job #24

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 🔹'
}
""".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'
}
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"
}
27 changes: 27 additions & 0 deletions test/scripts/Admin/seedSpec.groovy
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()
}

}