Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Clone all the jobs from a view (branch) to another view (branch,
- // performing all the required modifications.
- // Requires:
- // - Both source and target views to exist an be configured below.
- // - New runner, versions... to be already available
- import org.jenkinsci.plugins.workflow.job.WorkflowJob
- import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition
- // Configuration section.
- // View names.
- def sourceViewName = 'B - master'
- def targetViewName = 'B - 35'
- // Prefix to use in the names of the new jobs.
- def targetPrefix = '35'
- // Filter, in case we want to restrict to some job(s). Blank = no filter
- def filter = 'W.01.01'
- // End of configuration section.
- // Verify views exist.
- def sourceView = Jenkins.instance.getView(sourceViewName)
- if (sourceView == null) {
- println('Configuration error: sourceViewName view does not exist')
- return
- }
- def targetView = Jenkins.instance.getView(targetViewName)
- if (targetView == null) {
- println('Configuration error: targetViewName view does not exist')
- return
- }
- // Iterate over all the projects in the source view.
- for (item in sourceView.getItems()) {
- def name = item.getName()
- println('Processing "' + name + '"')
- // Apply filter if existing.
- if (filter != '' && !name.contains(filter)) {
- println(' Skipped, does not match "' + filter + "'")
- continue
- }
- // Calculate new name and verify the job doesn't exist.
- def newName = name.substring(0,1) + targetPrefix + name.substring(1)
- if (Jenkins.instance.getItem(newName)) {
- println(' Skipped, already exits "' + newName + '"')
- continue
- }
- println (' Creating new job "' + newName + "'")
- // Copy, disable and save.
- def job = Jenkins.instance.copy(item, newName)
- job.disabled = true
- println (' Job duplicated')
- // Apply pipeline replacements
- if (job.class.simpleName == 'WorkflowJob') {
- def script = job.getDefinition().getScript()
- // Master() by prefix()
- def search = 'Master()'
- def replacement = 'M' + targetPrefix + '()'
- script = script.replace(search, replacement)
- job.setDefinition(new CpsFlowDefinition(script))
- println (' Script adapted: "' + search + '" => "' + replacement + '"')
- }
- // Save the job
- job.save()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement