Commit 399b7305 authored by Eloy Lafuente's avatar Eloy Lafuente
Browse files

Improved Cloner Task

It supports all the original CloneMasterViewTask supports plus:

- Clone not only FROM master but from any numerical job (W25, S36, MW37...)
- Cloning MW jobs and MW versions (WP37...)

This job has been already created @ ci.moodle.org to use this Task:

https://ci.moodle.org/view/maintenance/job/MAINT%20-%20Clone%20jobs%20from%20view/
parent 3ab317bf
package org.moodle.ci.maintenance;
import org.jenkinsci.plugins.workflow.job.WorkflowJob
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition
import hudson.model.ParametersDefinitionProperty
/**
* Task to easily clone jobs from a view to another
*
* Clone jobs from a view/branch to another view/branch,
* performing all the required modifications.
*
* Requires:
* - Both source and target views to exist and be configured below.
* - New runner, versions... of scripts to be already available.
* Supports:
* - Specify source and target view name
* - Filter jobs by regex name matching.
*/
class CloneJobsFromViewTask {
/**
* Source view name, from where jobs will be picked. Must exist.
*/
protected String sourceViewName = 'B - master'
/**
* Configurable name of the target view. Must exist.
*/
protected String targetViewName = 'B - 35'
/**
* Prefix to use in the name of the new jobs (should match view name number).
*/
protected String targetPrefix = '35'
/**
* Filter (substring matching) to restrict the clone operation.
*/
protected filter = 'S.99.99'
/**
* Run the configured clone operation.
*/
int run(Script out) {
// Verify views exist.
def sourceView = Jenkins.instance.getView(sourceViewName)
if (sourceView == null) {
out.println 'Configuration error: sourceViewName view does not exist'
return 1
}
def targetView = Jenkins.instance.getView(targetViewName)
if (targetView == null) {
out.println 'Configuration error: targetViewName view does not exist'
return 1
}
// Iterate over all the projects in the source view.
for (item in sourceView.getItems()) {
def name = item.getName()
out.println 'Processing "' + name + '"'
// Apply filter if existing.
if (filter != '' && !name.contains(filter)) {
continue
}
// Calculate new name and verify the job doesn't exist.
def match = (name =~ /^([A-Z]+)([0-9]*)(\..*)$/)
def namePrefix = ''
def nameNumber = ''
def nameSuffix = ''
if (match.find()) {
namePrefix = match.group(1)
nameNumber = match.group(2)
nameSuffix = match.group(3)
} else {
out.println ' Skipped, unable to split job name into prefix and suffix'
continue
}
def newName = namePrefix + targetPrefix + nameSuffix
if (Jenkins.instance.getItem(newName)) {
out.println ' Skipped, already exits "' + newName + '"'
continue
}
out.println ' Creating new job "' + newName + "'"
// Copy, disable and save.
def job = Jenkins.instance.copy(item, newName)
job.disabled = true
out.println ' Job created'
// Apply pipeline replacements
if (job.class.simpleName == 'WorkflowJob') {
def script = job.getDefinition().getScript()
// Replace the moodleVersion to the target one.
def search = ''
def replacement = ''
// If the job is a Moodle one (W or S)
if (namePrefix == 'W' || namePrefix == 'S') {
if (nameNumber == '') { // The source is a master job.
search = 'Master()'
replacement = 'M' + targetPrefix + '()'
} else { // The source is a numerical branch job
search = 'M' + nameNumber + '()'
replacement = 'M' + targetPrefix + '()'
}
// If the job is a Workplace one (MW)
} else if (namePrefix == 'MW') {
if (nameNumber == '') { // The source is a master job.
search = 'Master()'
replacement = 'WP' + targetPrefix + '()'
} else { // The source is a numerical branch job
search = 'WP' + nameNumber + '()'
replacement = 'WP' + targetPrefix + '()'
}
}
script = script.replace(search, replacement)
job.setDefinition(new CpsFlowDefinition(script, true))
out.println ' Pipeline script adapted: "' + search + '" => "' + replacement + '"'
}
// Apply paramenter replacements
job.getProperties().values().each { property ->
if (property instanceof ParametersDefinitionProperty) {
property.getParameterDefinitionNames().each { paramName ->
// Any parameter which name contains "branch" and with default value = "master"
// is replaced by new MOODLE_XX_STABLE default value
if (paramName.contains("branch")) {
def paramDefaultValue = property.getParameterDefinition(paramName).getDefaultParameterValue().getValue()
if (paramDefaultValue == 'master') {
def newDefaultValue = 'MOODLE_' + targetPrefix + '_STABLE'
property.getParameterDefinition(paramName).setDefaultValue(newDefaultValue)
out.println ' Parameter "' + paramName + '" default changed: "master" => "' + newDefaultValue + '"'
}
}
}
}
}
// Save the job
job.save()
}
return 0
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment