diff --git a/build.gradle b/build.gradle index 96cbe269..62b47829 100644 --- a/build.gradle +++ b/build.gradle @@ -38,7 +38,7 @@ task syncWithRepo(dependsOn: 'classes', type: JavaExec) { main = 'com.entagen.jenkins.Main' classpath = sourceSets.main.runtimeClasspath // pass through specified system properties to the call to main - ['help', 'jenkinsUrl', 'jenkinsUser', 'jenkinsPassword', 'gitUrl', 'templateJobPrefix', 'templateBranchName', 'branchNameRegex', 'nestedView', 'viewRegex', 'printConfig', 'dryRun', 'startOnCreate', 'noViews', 'noDelete', 'allowSelfsignedSslCerts'].each { + ['help', 'jenkinsUrl', 'jenkinsUser', 'jenkinsPassword', 'gitUrl', 'templateJobPrefix', 'templateBranchName', 'branchNameRegex', 'nestedView', 'viewRegex', 'printConfig', 'dryRun', 'startOnCreate', 'noViews', 'noDelete', 'allowSelfsignedSslCerts', 'enableJobRegex', 'disableJobRegex'].each { if (System.getProperty(it)) systemProperty it, System.getProperty(it) } diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy index c61fa8c8..b7497e3a 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy @@ -61,7 +61,8 @@ class JenkinsApi { headers: [Accept: 'application/xml']) response.data.text } - void cloneJobForBranch(ConcreteJob missingJob, List templateJobs) { + + boolean cloneJobForBranch(ConcreteJob missingJob, List templateJobs, String enableJobRegex, String disableJobRegex) { String missingJobConfig = configForMissingJob(missingJob, templateJobs) TemplateJob templateJob = missingJob.templateJob @@ -72,8 +73,20 @@ class JenkinsApi { //Forced disable enable to work around Jenkins' automatic disabling of clones jobs //But only if the original job was enabled post('job/' + missingJob.jobName + '/disable') + + if (missingJob.jobName =~ disableJobRegex) { + println "Disabling ${missingJob.jobName} as it matches disableJobRegex (${disableJobRegex})" + return false + } + if (missingJob.jobName =~ enableJobRegex) { + println "Enabling ${missingJob.jobName} as it matches enableJobRegex (${enableJobRegex})" + post('job/' + missingJob.jobName + '/enable') + return true + } if (!missingJobConfig.contains("true")) { + println "Enabling ${missingJob.jobName} as the template job is enabled" post('job/' + missingJob.jobName + '/enable') + return true } } diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy index 8e3e7da1..b051813e 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy @@ -10,6 +10,8 @@ class JenkinsJobManager { String viewRegex String jenkinsUser String jenkinsPassword + String enableJobRegex + String disableJobRegex String startOnCreate Boolean allowSelfsignedSslCerts = false @@ -61,8 +63,8 @@ class JenkinsJobManager { for(ConcreteJob missingJob in missingJobs) { println "Creating missing job: ${missingJob.jobName} from ${missingJob.templateJob.jobName}" - jenkinsApi.cloneJobForBranch(missingJob, templateJobs) - if (startOnCreate) { + boolean enabled = jenkinsApi.cloneJobForBranch(missingJob, templateJobs, enableJobRegex, disableJobRegex) + if (enabled && startOnCreate) { jenkinsApi.startJob(missingJob, startOnCreate) } } diff --git a/src/main/groovy/com/entagen/jenkins/Main.groovy b/src/main/groovy/com/entagen/jenkins/Main.groovy index e850bc2f..c74f16ff 100644 --- a/src/main/groovy/com/entagen/jenkins/Main.groovy +++ b/src/main/groovy/com/entagen/jenkins/Main.groovy @@ -19,6 +19,8 @@ class Main { r: [longOpt: 'view-regex', required: false, args: 1, argName: 'viewRegex', description: "Supply a custom regex to be applied to any generated views, overriding the default template regex - gradle flag: -DviewRegex="], k: [longOpt: 'no-delete', required: false, args: 0, argName: 'noDelete', description: "Do not delete (keep) branches and views - gradle flag -DnoDelete=true"], f: [longOpt: 'filter-branch-names', required: false, args: 1, argName: 'branchNameRegex', description: "Only branches matching the regex will be accepted - gradle flag: -DbranchNameRegex="], + en: [longOpt: 'enable-job-regex', required: false, args: 1, argName: 'enableJobRegex', description: "If job matches regex, enable job no matter status of template job"], + dis: [longOpt: 'disable-job-regex', required: false, args: 1, argName: 'disableJobRegex', description: "If job matches regex, disable job no matter status of template job"], usr: [longOpt: 'jenkins-user', required: false, args: 1, argName: 'jenkinsUser', description: "Jenkins username - gradle flag -DjenkinsUser="], pwd: [longOpt: 'jenkins-password', required: false, args: 1, argName: 'jenkinsPassword', description: "Jenkins password - gradle flag -DjenkinsPassword="], selfsigned: [longOpt: 'allow-selfsigned-ssl-certs', required:false, args: 0, argName:'allowSelfsignedSslCerts', description: "Allow self signed ssl certificats for Jenkins API calls"]