Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Add regular expression flags to either disable or enable cloned jobs #65

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
14 changes: 13 additions & 1 deletion src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class JenkinsApi {
response.data.text
}

void cloneJobForBranch(ConcreteJob missingJob, List<TemplateJob> templateJobs) {
boolean cloneJobForBranch(ConcreteJob missingJob, List<TemplateJob> templateJobs, String enableJobRegex, String disableJobRegex) {
String missingJobConfig = configForMissingJob(missingJob, templateJobs)
TemplateJob templateJob = missingJob.templateJob

Expand All @@ -62,8 +62,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("<disabled>true</disabled>")) {
println "Enabling ${missingJob.jobName} as the template job is enabled"
post('job/' + missingJob.jobName + '/enable')
return true
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class JenkinsJobManager {
String branchNameRegex
String jenkinsUser
String jenkinsPassword
String enableJobRegex
String disableJobRegex

Boolean dryRun = false
Boolean noViews = false
Expand Down Expand Up @@ -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)
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/groovy/com/entagen/jenkins/Main.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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=<regex>"],
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=<jenkinsUser>"],
pwd: [longOpt: 'jenkins-password', required: false, args: 1, argName: 'jenkinsPassword', description: "Jenkins password - gradle flag -DjenkinsPassword=<jenkinsPassword>"]
]
Expand Down