From 6cda5eac914905a8ea4bace2cd950aefc5d33876 Mon Sep 17 00:00:00 2001 From: Den Markin Date: Thu, 1 Aug 2013 12:28:57 -0700 Subject: [PATCH 01/14] wipe out workspace before deleting jobs --- src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy | 5 +++++ src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy index e5bdc589..10e89559 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy @@ -95,6 +95,11 @@ class JenkinsApi { println "deleting job $jobName" post("job/${jobName}/doDelete") } + + void wipeOutWorkspace(String jobName) { + println "wiping out workspace for job $jobName" + post("job/${jobName}/doWipeOutWorkspace") + } void createViewForBranch(BranchView branchView, String nestedWithinView = null) { String viewName = branchView.viewName diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy index 80986eb1..61230e47 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy @@ -11,7 +11,7 @@ class JenkinsJobManager { String branchNameRegex String jenkinsUser String jenkinsPassword - + Boolean dryRun = false Boolean noViews = false Boolean noDelete = false @@ -73,6 +73,7 @@ class JenkinsJobManager { if (!deprecatedJobNames) return println "Deleting deprecated jobs:\n\t${deprecatedJobNames.join('\n\t')}" deprecatedJobNames.each { String jobName -> + jenkinsApi.wipeOutWorkspace(jobName) jenkinsApi.deleteJob(jobName) } } From 6918f1cf8c38b8447d0982c2ba1ffe589d6871c7 Mon Sep 17 00:00:00 2001 From: Paul Huck Date: Tue, 6 Aug 2013 13:48:12 -0700 Subject: [PATCH 02/14] enable jobs after creation --- src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy | 5 +++++ src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy | 1 + 2 files changed, 6 insertions(+) diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy index 10e89559..b2f84f18 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy @@ -101,6 +101,11 @@ class JenkinsApi { post("job/${jobName}/doWipeOutWorkspace") } + void enableJob(String jobName) { + println "enable job $jobName" + post("job/${jobName}/enable") + } + void createViewForBranch(BranchView branchView, String nestedWithinView = null) { String viewName = branchView.viewName Map body = [name: viewName, mode: 'hudson.model.ListView', Submit: 'OK', json: '{"name": "' + viewName + '", "mode": "hudson.model.ListView"}'] diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy index 61230e47..a26057ac 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy @@ -62,6 +62,7 @@ class JenkinsJobManager { for(ConcreteJob missingJob in missingJobs) { println "Creating missing job: ${missingJob.jobName} from ${missingJob.templateJob.jobName}" jenkinsApi.cloneJobForBranch(missingJob, templateJobs) + jenkinsApi.enableJob(missingJob.jobName) if (startOnCreate) { jenkinsApi.startJob(missingJob) } From 42ec45fb8fc729efaf6479441f7063ec5f3c6ccb Mon Sep 17 00:00:00 2001 From: Bassam Mehanni Date: Fri, 16 Aug 2013 14:42:52 -0700 Subject: [PATCH 03/14] Added support for feature cleanup --- build.gradle | 2 +- .../com/entagen/jenkins/JenkinsApi.groovy | 10 ++++----- .../entagen/jenkins/JenkinsJobManager.groovy | 21 ++++++++++++++++--- .../groovy/com/entagen/jenkins/Main.groovy | 4 +++- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/build.gradle b/build.gradle index db3b097a..8d54e5f7 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', 'cleanupJobName', 'deployJobBaseName'].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 b2f84f18..b5087ab1 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy @@ -65,6 +65,11 @@ class JenkinsApi { println "Starting job ${job.jobName}." post('job/' + job.jobName + '/build') } + + void startJobWithParameters(String jobName, postBody = [:]) { + println "Starting job ${jobName}." + post('job/' + jobName + '/build', postBody) + } String configForMissingJob(ConcreteJob missingJob, List templateJobs) { TemplateJob templateJob = missingJob.templateJob @@ -201,11 +206,6 @@ class JenkinsApi { params[crumbInfo.field] = crumbInfo.crumb } - - - - - HTTPBuilder http = new HTTPBuilder(jenkinsServerUrl) if (requestInterceptor) { diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy index a26057ac..25375f7b 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy @@ -1,6 +1,7 @@ package com.entagen.jenkins import java.util.regex.Pattern +import groovy.json.JsonSlurper class JenkinsJobManager { String templateJobPrefix @@ -11,7 +12,9 @@ class JenkinsJobManager { String branchNameRegex String jenkinsUser String jenkinsPassword - + String cleanupJobName + String deployJobBaseName + Boolean dryRun = false Boolean noViews = false Boolean noDelete = false @@ -48,7 +51,7 @@ class JenkinsJobManager { List currentTemplateDrivenJobNames = templateDrivenJobNames(templateJobs, allJobNames) List nonTemplateBranchNames = allBranchNames - templateBranchName List expectedJobs = this.expectedJobs(templateJobs, nonTemplateBranchNames) - + createMissingJobs(expectedJobs, currentTemplateDrivenJobNames, templateJobs) if (!noDelete) { deleteDeprecatedJobs(currentTemplateDrivenJobNames - expectedJobs.jobName) @@ -67,7 +70,6 @@ class JenkinsJobManager { jenkinsApi.startJob(missingJob) } } - } public void deleteDeprecatedJobs(List deprecatedJobNames) { @@ -77,6 +79,19 @@ class JenkinsJobManager { jenkinsApi.wipeOutWorkspace(jobName) jenkinsApi.deleteJob(jobName) } + + if (cleanupJobName != null) { + List deployFeatureNames = deprecatedJobNames.collectMany{ it.startsWith(deployJobBaseName) ? [it.replace(deployJobBaseName, '')] : [] } + + if (deployFeatureNames.size > 0) { + String features = deployFeatureNames.join(',') + println "Cleaning up Features:$features using $cleanupJobName" + + def body = [:] + body = [json: '{"parameter":[{"name": "Features", "value": "' + features + '"}]}'] + jenkinsApi.startJobWithParameters(cleanupJobName, body) + } + } } public List expectedJobs(List templateJobs, List branchNames) { diff --git a/src/main/groovy/com/entagen/jenkins/Main.groovy b/src/main/groovy/com/entagen/jenkins/Main.groovy index 5e7b1fa0..3521cb8c 100644 --- a/src/main/groovy/com/entagen/jenkins/Main.groovy +++ b/src/main/groovy/com/entagen/jenkins/Main.groovy @@ -19,7 +19,9 @@ class Main { 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="], 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="] + pwd: [longOpt: 'jenkins-password', required: false, args: 1, argName: 'jenkinsPassword', description: "Jenkins password - gradle flag -DjenkinsPassword="], + cu: [longOpt: 'cleanup-job-name', required: false, args: 1, argName: 'cleanupJobName', description: "Deploy Cleanup Job Name - gradle flag -DcleanupJobName="], + jd: [longOpt: 'deploy-base-job-name', required: false, args: 1, argName: 'deployJobBaseName', description: "Deploy base Job Name - gradle flag -DdeployJobBaseName="], ] public static void main(String[] args) { From a948198142c687708eba7657b91c5b94c50d6bf6 Mon Sep 17 00:00:00 2001 From: Bassam Mehanni Date: Tue, 20 Aug 2013 16:36:00 -0700 Subject: [PATCH 04/14] Cleanup feature name for the feature deploy --- src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy index 25375f7b..0fe748b9 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy @@ -81,7 +81,7 @@ class JenkinsJobManager { } if (cleanupJobName != null) { - List deployFeatureNames = deprecatedJobNames.collectMany{ it.startsWith(deployJobBaseName) ? [it.replace(deployJobBaseName, '')] : [] } + List deployFeatureNames = deprecatedJobNames.collectMany{ it.contains('deploy') ? [it.replace(deployJobBaseName, '').replace('-', '_').replace(' ', '')] : [] } if (deployFeatureNames.size > 0) { String features = deployFeatureNames.join(',') From 4e8489072f52c61761fd94cc46efdcff1c1c8f49 Mon Sep 17 00:00:00 2001 From: Bassam Mehanni Date: Fri, 3 Jan 2014 13:54:37 -0800 Subject: [PATCH 05/14] Fixed a bug in filtering out deploy jobs --- src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy index 0fe748b9..fd2ce64a 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy @@ -81,7 +81,7 @@ class JenkinsJobManager { } if (cleanupJobName != null) { - List deployFeatureNames = deprecatedJobNames.collectMany{ it.contains('deploy') ? [it.replace(deployJobBaseName, '').replace('-', '_').replace(' ', '')] : [] } + List deployFeatureNames = deprecatedJobNames.collectMany{ it.startsWith(deployJobBaseName) ? [it.replace(deployJobBaseName, '').replace('-', '_').replace(' ', '')] : [] } if (deployFeatureNames.size > 0) { String features = deployFeatureNames.join(',') From 7ec9075cd746a5ced46638c5b8d222c2e452e65f Mon Sep 17 00:00:00 2001 From: Bassam Mehanni Date: Sat, 2 Aug 2014 15:09:14 -0700 Subject: [PATCH 06/14] FOX-6278 The Sync feature branch jenkins job breaks if there are branches with the same name but different case --- src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy index fd2ce64a..ba67a9c0 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy @@ -32,7 +32,7 @@ class JenkinsJobManager { } void syncWithRepo() { - List allBranchNames = gitApi.branchNames + List allBranchNames = gitApi.branchNames.unique{ it.toLowerCase() } List allJobNames = jenkinsApi.jobNames // ensure that there is at least one job matching the template pattern, collect the set of template jobs From 0c696c607ff42f46449a2be4d7196a630f5f6b9f Mon Sep 17 00:00:00 2001 From: Paul Huck Date: Wed, 20 Aug 2014 08:24:32 -0700 Subject: [PATCH 07/14] FOX-6050 cleanup code removed groovy Cleanup is kicked off as a separate build step in Jenkins. Added script to create properties file for the cleanup job --- Write-Feature-Properties.ps1 | 20 +++++++++++++++++++ .../entagen/jenkins/JenkinsJobManager.groovy | 13 ------------ 2 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 Write-Feature-Properties.ps1 diff --git a/Write-Feature-Properties.ps1 b/Write-Feature-Properties.ps1 new file mode 100644 index 00000000..d4584619 --- /dev/null +++ b/Write-Feature-Properties.ps1 @@ -0,0 +1,20 @@ +<# +.SYNOPSIS + Retrieves the list of feature branches and writes them to a property file of the form + Features=feature_xxxx,feaure_yyyy,feature_zzzz + +.EXAMPLE + Write-FeatureProperties -GitUrl https://user:password@github.com/revdotcom/revdotcom -OutFile branches.properties +#> +[CmdletBinding()] +param( + [Parameter(Mandatory=$True)] [string] $GitUrl, + [Parameter(Mandatory=$True)] [string] $OutFile +) + +$branches = git ls-remote --heads $GitUrl +if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } +$features = $branches | ? { $_ -match "\s+refs/heads/feature/(.*)$" } | % { "feature_" + $matches[1] } +$features = $features -Join ',' +#use ASCII to avoid Out-File writing a Byte Order Mark +echo Features=$features | Out-File -Encoding ascii $OutFile diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy index ba67a9c0..d5132b59 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy @@ -79,19 +79,6 @@ class JenkinsJobManager { jenkinsApi.wipeOutWorkspace(jobName) jenkinsApi.deleteJob(jobName) } - - if (cleanupJobName != null) { - List deployFeatureNames = deprecatedJobNames.collectMany{ it.startsWith(deployJobBaseName) ? [it.replace(deployJobBaseName, '').replace('-', '_').replace(' ', '')] : [] } - - if (deployFeatureNames.size > 0) { - String features = deployFeatureNames.join(',') - println "Cleaning up Features:$features using $cleanupJobName" - - def body = [:] - body = [json: '{"parameter":[{"name": "Features", "value": "' + features + '"}]}'] - jenkinsApi.startJobWithParameters(cleanupJobName, body) - } - } } public List expectedJobs(List templateJobs, List branchNames) { From 12ccc585c2adbc7bfca6b186f6a970d6bc5d58ce Mon Sep 17 00:00:00 2001 From: Paul Huck Date: Mon, 6 Oct 2014 08:17:40 -0700 Subject: [PATCH 08/14] delete unused variables --- src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy index d5132b59..973bc187 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy @@ -12,9 +12,7 @@ class JenkinsJobManager { String branchNameRegex String jenkinsUser String jenkinsPassword - String cleanupJobName - String deployJobBaseName - + Boolean dryRun = false Boolean noViews = false Boolean noDelete = false From b7db064d3b553546b53a7e1dad682957e74059ef Mon Sep 17 00:00:00 2001 From: Bryan Roscoe Date: Tue, 22 Mar 2016 16:11:39 -0500 Subject: [PATCH 09/14] FOX-6280:Change feature branches to - not _ FOX-6280:Remove feature prefix from this script --- Write-Feature-Properties.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Write-Feature-Properties.ps1 b/Write-Feature-Properties.ps1 index d4584619..d851b04f 100644 --- a/Write-Feature-Properties.ps1 +++ b/Write-Feature-Properties.ps1 @@ -14,7 +14,7 @@ param( $branches = git ls-remote --heads $GitUrl if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } -$features = $branches | ? { $_ -match "\s+refs/heads/feature/(.*)$" } | % { "feature_" + $matches[1] } +$features = $branches | ? { $_ -match "\s+refs/heads/feature/(.*)$" } | % { $matches[1] } $features = $features -Join ',' #use ASCII to avoid Out-File writing a Byte Order Mark echo Features=$features | Out-File -Encoding ascii $OutFile From 6dc6e5a5545f332912f2362885cf178d86988796 Mon Sep 17 00:00:00 2001 From: bryanroscoe Date: Tue, 3 May 2016 12:36:14 -0500 Subject: [PATCH 10/14] FOX-6280:Only start build branches (#2) * FOX-6280:Only start build branches * FOX-6280:Stop job while deleting branch * FOX-6280: Refactor retry --- .../groovy/com/entagen/jenkins/JenkinsApi.groovy | 5 +++++ .../com/entagen/jenkins/JenkinsJobManager.groovy | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy index b5087ab1..e783e09c 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy @@ -100,6 +100,11 @@ class JenkinsApi { println "deleting job $jobName" post("job/${jobName}/doDelete") } + + void stopJob(String jobName) { + println "Stopping $jobName" + post("job/${jobName}/lastBuild/stop") + } void wipeOutWorkspace(String jobName) { println "wiping out workspace for job $jobName" diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy index 973bc187..6ffb1d00 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy @@ -64,7 +64,7 @@ class JenkinsJobManager { println "Creating missing job: ${missingJob.jobName} from ${missingJob.templateJob.jobName}" jenkinsApi.cloneJobForBranch(missingJob, templateJobs) jenkinsApi.enableJob(missingJob.jobName) - if (startOnCreate) { + if (startOnCreate && missingJob.jobName.contains("Rev.com-featurebranch-build-feature")) { jenkinsApi.startJob(missingJob) } } @@ -74,7 +74,17 @@ class JenkinsJobManager { if (!deprecatedJobNames) return println "Deleting deprecated jobs:\n\t${deprecatedJobNames.join('\n\t')}" deprecatedJobNames.each { String jobName -> - jenkinsApi.wipeOutWorkspace(jobName) + try { + jenkinsApi.wipeOutWorkspace(jobName) + } + catch(Exception ex) { + println "Attempting to stop $jobName since wiping out the workspace failed" + jenkinsApi.stopJob(jobName) + println "Giving $jobName 15 seconds before wiping out the workspace again" + sleep(15000) + jenkinsApi.wipeOutWorkspace(jobName) + } + jenkinsApi.deleteJob(jobName) } } From 1ca7f280070ae33ca8526be1e1eb1e715f8416c5 Mon Sep 17 00:00:00 2001 From: bryanroscoe Date: Wed, 24 Aug 2016 11:36:28 -0500 Subject: [PATCH 11/14] FOX-11886:Ignore case when creating new branches (#3) --- .../groovy/com/entagen/jenkins/JenkinsJobManager.groovy | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy index 6ffb1d00..2b4c63b1 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy @@ -49,7 +49,7 @@ class JenkinsJobManager { List currentTemplateDrivenJobNames = templateDrivenJobNames(templateJobs, allJobNames) List nonTemplateBranchNames = allBranchNames - templateBranchName List expectedJobs = this.expectedJobs(templateJobs, nonTemplateBranchNames) - + createMissingJobs(expectedJobs, currentTemplateDrivenJobNames, templateJobs) if (!noDelete) { deleteDeprecatedJobs(currentTemplateDrivenJobNames - expectedJobs.jobName) @@ -57,7 +57,8 @@ class JenkinsJobManager { } public void createMissingJobs(List expectedJobs, List currentJobs, List templateJobs) { - List missingJobs = expectedJobs.findAll { !currentJobs.contains(it.jobName) } + List lowercaseCurrentJobs = currentJobs.collect()*.toLowerCase() + List missingJobs = expectedJobs.findAll { !lowercaseCurrentJobs.contains(it.jobName.toLowerCase()) } if (!missingJobs) return for(ConcreteJob missingJob in missingJobs) { @@ -84,7 +85,7 @@ class JenkinsJobManager { sleep(15000) jenkinsApi.wipeOutWorkspace(jobName) } - + jenkinsApi.deleteJob(jobName) } } From 295daaefdb109afedc11450aa91645702e16e9f8 Mon Sep 17 00:00:00 2001 From: Kevin Gann Date: Wed, 22 Mar 2017 12:44:21 -0500 Subject: [PATCH 12/14] FOX-13767 support unit & feature branches (#4) --- build.gradle | 4 +++- .../com/entagen/jenkins/JenkinsApi.groovy | 6 +++-- .../entagen/jenkins/JenkinsJobManager.groovy | 23 +++++++++++++++++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index 8d54e5f7..3db11e35 100644 --- a/build.gradle +++ b/build.gradle @@ -33,7 +33,9 @@ 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', 'cleanupJobName', 'deployJobBaseName'].each { + ['help', 'jenkinsUrl', 'jenkinsUser', 'jenkinsPassword', 'gitUrl', 'templateJobPrefix', 'templateBranchName', + 'branchNameRegex', 'nestedView', 'printConfig', 'dryRun', 'startOnCreate', 'noViews', 'noDelete', + 'cleanupJobName', 'deployJobBaseName', 'unitTestOnly'].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 e783e09c..8b26741f 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy @@ -46,6 +46,8 @@ class JenkinsApi { } String getJobConfig(String jobName) { + // Please note that for this to work it requires that the authenticated user have ExtendedRead permission or it + // will return HTTP 403 (Forbidden). See `/role-strategy/manage-roles`. def response = get(path: "job/${jobName}/config.xml", contentType: TEXT, headers: [Accept: 'application/xml']) response.data.text @@ -62,12 +64,12 @@ class JenkinsApi { } void startJob(ConcreteJob job) { - println "Starting job ${job.jobName}." + println "Starting job ${job.jobName}" post('job/' + job.jobName + '/build') } void startJobWithParameters(String jobName, postBody = [:]) { - println "Starting job ${jobName}." + println "Starting job ${jobName}" post('job/' + jobName + '/build', postBody) } diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy index 2b4c63b1..c6d18945 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy @@ -4,6 +4,7 @@ import java.util.regex.Pattern import groovy.json.JsonSlurper class JenkinsJobManager { + String templateJobPrefix String templateBranchName String gitUrl @@ -63,9 +64,27 @@ class JenkinsJobManager { for(ConcreteJob missingJob in missingJobs) { println "Creating missing job: ${missingJob.jobName} from ${missingJob.templateJob.jobName}" + + // This creates missing jobs using the missingJob name. Note that templateJobs is a List. jenkinsApi.cloneJobForBranch(missingJob, templateJobs) - jenkinsApi.enableJob(missingJob.jobName) - if (startOnCreate && missingJob.jobName.contains("Rev.com-featurebranch-build-feature")) { + + // Rev.com-branch is -DtemplateJobPrefix + // Rev.com-branch-deploy- is the Jenkins job template + // Rev.com-branch-build- is the Jenkins job template + + if (missingJob.jobName.contains("Rev.com-build-unittest")) { + // If the job contains unittest enable it. Jobs start out disabled so -deploy- will remain disabled. + jenkinsApi.enableJob(missingJob.jobName) + } + if (!missingJob.jobName.contains("Rev.com-deploy-unittest")) { + // Prevent double enabling of unittest jobs. + if (!missingJob.jobName.contains("Rev.com-build-unittest")) { + // If the job doesn't contain unittest enable it. Jobs start out disabled. + jenkinsApi.enableJob(missingJob.jobName) + } + } + if (startOnCreate && missingJob.jobName.contains("Rev.com-build-")) { + // Starting -build- has the downstream job of -deploy- when successful. jenkinsApi.startJob(missingJob) } } From 75b663e32815b497892b5b8e36515d39eb0345f3 Mon Sep 17 00:00:00 2001 From: cyjing Date: Tue, 25 Jul 2017 10:32:42 -0700 Subject: [PATCH 13/14] FOX-18061 prevent syncgitbrancheswithjenkins from failing on error --- .../entagen/jenkins/JenkinsJobManager.groovy | 59 +++++++++++-------- .../groovy/com/entagen/jenkins/Main.groovy | 7 ++- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy index c6d18945..dc205037 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy @@ -3,6 +3,8 @@ package com.entagen.jenkins import java.util.regex.Pattern import groovy.json.JsonSlurper +Boolean failed = false + class JenkinsJobManager { String templateJobPrefix @@ -30,7 +32,9 @@ class JenkinsJobManager { initGitApi() } - void syncWithRepo() { + Boolean syncWithRepo() { + failed = false + List allBranchNames = gitApi.branchNames.unique{ it.toLowerCase() } List allJobNames = jenkinsApi.jobNames @@ -44,6 +48,8 @@ class JenkinsJobManager { if (!noViews) { syncViews(allBranchNames) } + + return failed } public void syncJobs(List allBranchNames, List allJobNames, List templateJobs) { @@ -64,28 +70,34 @@ class JenkinsJobManager { for(ConcreteJob missingJob in missingJobs) { println "Creating missing job: ${missingJob.jobName} from ${missingJob.templateJob.jobName}" + try { + // This creates missing jobs using the missingJob name. Note that templateJobs is a List. + jenkinsApi.cloneJobForBranch(missingJob, templateJobs) - // This creates missing jobs using the missingJob name. Note that templateJobs is a List. - jenkinsApi.cloneJobForBranch(missingJob, templateJobs) - - // Rev.com-branch is -DtemplateJobPrefix - // Rev.com-branch-deploy- is the Jenkins job template - // Rev.com-branch-build- is the Jenkins job template + // Rev.com-branch is -DtemplateJobPrefix + // Rev.com-branch-deploy- is the Jenkins job template + // Rev.com-branch-build- is the Jenkins job template - if (missingJob.jobName.contains("Rev.com-build-unittest")) { - // If the job contains unittest enable it. Jobs start out disabled so -deploy- will remain disabled. - jenkinsApi.enableJob(missingJob.jobName) - } - if (!missingJob.jobName.contains("Rev.com-deploy-unittest")) { - // Prevent double enabling of unittest jobs. - if (!missingJob.jobName.contains("Rev.com-build-unittest")) { - // If the job doesn't contain unittest enable it. Jobs start out disabled. + if (missingJob.jobName.contains("Rev.com-build-unittest")) { + // If the job contains unittest enable it. Jobs start out disabled so -deploy- will remain disabled. jenkinsApi.enableJob(missingJob.jobName) } + if (!missingJob.jobName.contains("Rev.com-deploy-unittest")) { + // Prevent double enabling of unittest jobs. + if (!missingJob.jobName.contains("Rev.com-build-unittest")) { + // If the job doesn't contain unittest enable it. Jobs start out disabled. + jenkinsApi.enableJob(missingJob.jobName) + } + } + if (startOnCreate && missingJob.jobName.contains("Rev.com-build-")) { + // Starting -build- has the downstream job of -deploy- when successful. + jenkinsApi.startJob(missingJob) + } } - if (startOnCreate && missingJob.jobName.contains("Rev.com-build-")) { - // Starting -build- has the downstream job of -deploy- when successful. - jenkinsApi.startJob(missingJob) + catch(Exception ex) { + println(ex.getMessage()); + println(ex.getStackTrace()); + failed = true } } } @@ -96,16 +108,13 @@ class JenkinsJobManager { deprecatedJobNames.each { String jobName -> try { jenkinsApi.wipeOutWorkspace(jobName) + jenkinsApi.deleteJob(jobName) } catch(Exception ex) { - println "Attempting to stop $jobName since wiping out the workspace failed" - jenkinsApi.stopJob(jobName) - println "Giving $jobName 15 seconds before wiping out the workspace again" - sleep(15000) - jenkinsApi.wipeOutWorkspace(jobName) + println(ex.getMessage()); + println(ex.getStackTrace()); + failed = true } - - jenkinsApi.deleteJob(jobName) } } diff --git a/src/main/groovy/com/entagen/jenkins/Main.groovy b/src/main/groovy/com/entagen/jenkins/Main.groovy index 3521cb8c..89e8d69a 100644 --- a/src/main/groovy/com/entagen/jenkins/Main.groovy +++ b/src/main/groovy/com/entagen/jenkins/Main.groovy @@ -28,7 +28,12 @@ class Main { Map argsMap = parseArgs(args) showConfiguration(argsMap) JenkinsJobManager manager = new JenkinsJobManager(argsMap) - manager.syncWithRepo() + + Boolean failure = manager.syncWithRepo() + + if (failure) { + System.exit(1); + } } public static Map parseArgs(String[] args) { From bb7142d9ba64823dbbd141eb38fb8f7df7e11c0b Mon Sep 17 00:00:00 2001 From: cyjing Date: Wed, 26 Jul 2017 13:19:49 -0700 Subject: [PATCH 14/14] FOX-18061 syntax --- .../com/entagen/jenkins/JenkinsJobManager.groovy | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy index dc205037..f28df6d5 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy @@ -3,8 +3,6 @@ package com.entagen.jenkins import java.util.regex.Pattern import groovy.json.JsonSlurper -Boolean failed = false - class JenkinsJobManager { String templateJobPrefix @@ -20,6 +18,7 @@ class JenkinsJobManager { Boolean noViews = false Boolean noDelete = false Boolean startOnCreate = false + Boolean failed = false JenkinsApi jenkinsApi GitApi gitApi @@ -33,7 +32,7 @@ class JenkinsJobManager { } Boolean syncWithRepo() { - failed = false + this.failed = false List allBranchNames = gitApi.branchNames.unique{ it.toLowerCase() } List allJobNames = jenkinsApi.jobNames @@ -49,7 +48,7 @@ class JenkinsJobManager { syncViews(allBranchNames) } - return failed + return this.failed } public void syncJobs(List allBranchNames, List allJobNames, List templateJobs) { @@ -97,7 +96,7 @@ class JenkinsJobManager { catch(Exception ex) { println(ex.getMessage()); println(ex.getStackTrace()); - failed = true + this.failed = true } } } @@ -113,7 +112,7 @@ class JenkinsJobManager { catch(Exception ex) { println(ex.getMessage()); println(ex.getStackTrace()); - failed = true + this.failed = true } } }