Skip to content

Commit

Permalink
Merge pull request entagen#65 from dhumeniuk/master
Browse files Browse the repository at this point in the history
Add regular expression flags to either disable or enable cloned jobs

* dhumeniuk/master:
  Revert removal of printing post body
  Update so start flag works for disabled jobs
  Add regular expression flags to either disable or enable cloned jobs
  • Loading branch information
andreineculau committed Aug 1, 2015
2 parents 73a2a14 + 2956051 commit b9fd79f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
15 changes: 14 additions & 1 deletion src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ class JenkinsApi {
headers: [Accept: 'application/xml'])
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 @@ -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("<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 @@ -10,6 +10,8 @@ class JenkinsJobManager {
String viewRegex
String jenkinsUser
String jenkinsPassword
String enableJobRegex
String disableJobRegex
String startOnCreate
Boolean allowSelfsignedSslCerts = 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, startOnCreate)
}
}
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 @@ -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=<regex>"],
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>"],
selfsigned: [longOpt: 'allow-selfsigned-ssl-certs', required:false, args: 0, argName:'allowSelfsignedSslCerts', description: "Allow self signed ssl certificats for Jenkins API calls"]
Expand Down

0 comments on commit b9fd79f

Please sign in to comment.