From 9cb514a8f0818a8376a0416dccd6567970dea683 Mon Sep 17 00:00:00 2001 From: "Humeniuk, Dave" Date: Fri, 27 Jun 2014 08:47:07 -0400 Subject: [PATCH 1/3] Add regular expression flags to either disable or enable cloned jobs --- build.gradle | 2 +- .../groovy/com/entagen/jenkins/JenkinsApi.groovy | 13 +++++++++++-- .../com/entagen/jenkins/JenkinsJobManager.groovy | 4 +++- src/main/groovy/com/entagen/jenkins/Main.groovy | 2 ++ 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index a0134abf..0be4f519 100644 --- a/build.gradle +++ b/build.gradle @@ -33,7 +33,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', 'printConfig', 'dryRun', 'startOnCreate', 'noViews', 'noDelete'].each { + ['help', 'jenkinsUrl', 'jenkinsUser', 'jenkinsPassword', 'gitUrl', 'templateJobPrefix', 'templateBranchName', 'branchNameRegex', 'nestedView', 'printConfig', 'dryRun', 'startOnCreate', 'noViews', 'noDelete', '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 d86852aa..23410430 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy @@ -51,7 +51,7 @@ class JenkinsApi { response.data.text } - void cloneJobForBranch(ConcreteJob missingJob, List templateJobs) { + void cloneJobForBranch(ConcreteJob missingJob, List templateJobs, String enableJobRegex, String disableJobRegex) { String missingJobConfig = configForMissingJob(missingJob, templateJobs) TemplateJob templateJob = missingJob.templateJob @@ -62,7 +62,16 @@ 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 (!missingJobConfig.contains("true")) { + + if (missingJob.jobName =~ disableJobRegex) { + println "Disabling ${missingJob.jobName} as it matches disableJobRegex (${disableJobRegex})" + } + else if (missingJob.jobName =~ enableJobRegex) { + println "Enabling ${missingJob.jobName} as it matches enableJobRegex (${enableJobRegex})" + post('job/' + missingJob.jobName + '/enable') + } + else if (!missingJobConfig.contains("true")) { + println "Enabling ${missingJob.jobName} as the template job is enabled" post('job/' + missingJob.jobName + '/enable') } } diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy index 80986eb1..5652aa33 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy @@ -11,6 +11,8 @@ class JenkinsJobManager { String branchNameRegex String jenkinsUser String jenkinsPassword + String enableJobRegex + String disableJobRegex Boolean dryRun = false Boolean noViews = false @@ -61,7 +63,7 @@ class JenkinsJobManager { for(ConcreteJob missingJob in missingJobs) { println "Creating missing job: ${missingJob.jobName} from ${missingJob.templateJob.jobName}" - jenkinsApi.cloneJobForBranch(missingJob, templateJobs) + jenkinsApi.cloneJobForBranch(missingJob, templateJobs, enableJobRegex, disableJobRegex) if (startOnCreate) { jenkinsApi.startJob(missingJob) } diff --git a/src/main/groovy/com/entagen/jenkins/Main.groovy b/src/main/groovy/com/entagen/jenkins/Main.groovy index 5e7b1fa0..2c1abaaf 100644 --- a/src/main/groovy/com/entagen/jenkins/Main.groovy +++ b/src/main/groovy/com/entagen/jenkins/Main.groovy @@ -18,6 +18,8 @@ class Main { v: [longOpt: 'no-views', required: false, args: 0, argName: 'noViews', description: "Suppress view creation - gradle flag -DnoViews=true"], 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="] ] From ead2619eb5081a2f474a6f625a7670f00212149e Mon Sep 17 00:00:00 2001 From: "Humeniuk, Dave" Date: Fri, 27 Jun 2014 10:12:28 -0400 Subject: [PATCH 2/3] Update so start flag works for disabled jobs --- src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy | 9 ++++++--- .../groovy/com/entagen/jenkins/JenkinsApiReadOnly.groovy | 2 +- .../groovy/com/entagen/jenkins/JenkinsJobManager.groovy | 4 ++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy index 23410430..5695a6c2 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy @@ -51,7 +51,7 @@ class JenkinsApi { response.data.text } - void cloneJobForBranch(ConcreteJob missingJob, List templateJobs, String enableJobRegex, String disableJobRegex) { + boolean cloneJobForBranch(ConcreteJob missingJob, List templateJobs, String enableJobRegex, String disableJobRegex) { String missingJobConfig = configForMissingJob(missingJob, templateJobs) TemplateJob templateJob = missingJob.templateJob @@ -65,14 +65,17 @@ class JenkinsApi { if (missingJob.jobName =~ disableJobRegex) { println "Disabling ${missingJob.jobName} as it matches disableJobRegex (${disableJobRegex})" + return false } - else if (missingJob.jobName =~ enableJobRegex) { + if (missingJob.jobName =~ enableJobRegex) { println "Enabling ${missingJob.jobName} as it matches enableJobRegex (${enableJobRegex})" post('job/' + missingJob.jobName + '/enable') + return true } - else if (!missingJobConfig.contains("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/JenkinsApiReadOnly.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsApiReadOnly.groovy index 0f168d1f..170d63db 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsApiReadOnly.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsApiReadOnly.groovy @@ -13,7 +13,7 @@ class JenkinsApiReadOnly extends JenkinsApi { @Override protected Integer post(String path, postBody = [:], params = [:], ContentType contentType = ContentType.URLENC) { - println "READ ONLY! skipping POST to $path with params: ${params}, postBody:\n$postBody" + println "READ ONLY! skipping POST to $path with params: ${params}" // we never want to post anything with a ReadOnly API, just return OK for all requests to it return HttpStatus.SC_OK } diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy index 5652aa33..e90b0d48 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy @@ -63,8 +63,8 @@ class JenkinsJobManager { for(ConcreteJob missingJob in missingJobs) { println "Creating missing job: ${missingJob.jobName} from ${missingJob.templateJob.jobName}" - jenkinsApi.cloneJobForBranch(missingJob, templateJobs, enableJobRegex, disableJobRegex) - if (startOnCreate) { + boolean enabled = jenkinsApi.cloneJobForBranch(missingJob, templateJobs, enableJobRegex, disableJobRegex) + if (enabled && startOnCreate) { jenkinsApi.startJob(missingJob) } } From 295605140ffbcd96e41b8d3fcae35e058bbd9695 Mon Sep 17 00:00:00 2001 From: "Humeniuk, Dave" Date: Fri, 27 Jun 2014 10:16:00 -0400 Subject: [PATCH 3/3] Revert removal of printing post body --- src/main/groovy/com/entagen/jenkins/JenkinsApiReadOnly.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsApiReadOnly.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsApiReadOnly.groovy index 170d63db..0f168d1f 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsApiReadOnly.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsApiReadOnly.groovy @@ -13,7 +13,7 @@ class JenkinsApiReadOnly extends JenkinsApi { @Override protected Integer post(String path, postBody = [:], params = [:], ContentType contentType = ContentType.URLENC) { - println "READ ONLY! skipping POST to $path with params: ${params}" + println "READ ONLY! skipping POST to $path with params: ${params}, postBody:\n$postBody" // we never want to post anything with a ReadOnly API, just return OK for all requests to it return HttpStatus.SC_OK }