diff --git a/jobs/Admin/seed/Jenkinsfile b/jobs/Admin/seed/Jenkinsfile new file mode 100644 index 0000000..21e5a8b --- /dev/null +++ b/jobs/Admin/seed/Jenkinsfile @@ -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 getDslFiles() { + findFiles(glob: 'jobs/**/Jenkinsfile.dsl') + .findAll { !it.directory } + .collect { it.path } + .toSorted() +} + +List 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/' +} diff --git a/jobs/Admin/seed/Jenkinsfile.dsl b/jobs/Admin/seed/Jenkinsfile.dsl new file mode 100644 index 0000000..66df670 --- /dev/null +++ b/jobs/Admin/seed/Jenkinsfile.dsl @@ -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" +}