I have tried to rename a hudson/jenkins job. However it failed to rename.
Is there any way so I can rename the job?
Just for the sake of completeness, want to mention the update of Hudson job name using Groovy script console:
// Groovy script to rename job in Hudson
import hudson.model.*;
def JOB_PATTERN = ~/^MY_JOB.*$/; //find all jobs starting with "MY_JOB".
def NEW_PART = "_NEW"
(Hudson.instance.items.findAll { job -> job.name =~ JOB_PATTERN }).each { job_to_update ->
println ("Updating job " + job_to_update.name);
def new_job_name = job_to_update.name + NEW_PART; //Append new part to the job name
println ("New name: " + new_job_name);
job_to_update.renameTo(new_job_name);
println ("Updated name: " + job_to_update.name);
println("="*80);
}
Rather helpful if you need to update several dozens of jobs at the same time.
Note: The following code will not work:
job_to_update.name = new_job_name;
job_to_update.save();
Setting the name of the job to new and saving configuration will not rename current job, but create a copy of the job configuration with a new name. Also, in this case, there might be broken references, so Hudson will need to have configuration reloaded.
The normal way to rename a job:
Go to the Configure screen and edit the Project name field right on top. Then click on Save and confirm by clicking on Yes. (do not click on the Apply button next to Save, that will give you an error message: JENKINS-17474)
Changing the Display Name will not rename the job, it only changes how it will be displayed. It will still be found under it's original name via the search box for example and that also will show up in the url.
Renaming directories on the filesystem level should really not be necessary.
I can't make Marc's script work, so write one based on Disable all jobs script as shown below. This is to rename any project with "Findur.OpenComponent" to "Findur.OpenComponents".
import hudson.model.*
renameChildren(Hudson.instance.items)
def renameChildren(items) {
for (item in items) {
if (item.class.canonicalName != 'com.cloudbees.hudson.plugins.folder.Folder') {
if (( m = item.name =~ /^(Findur.OpenComponent)(\..*)$/)){
println(item.name)
println m.group(1) + " " + m.group(2)
newname = m[0][1] + 's' + m.group(2)
item.renameTo(newname)
}
} else {
renameChildren(((com.cloudbees.hudson.plugins.folder.Folder) item).getItems())
}
}
}
Use the function Job.previousNames()
to rename multiple jobs using Groovy script within a [job generator](Job Generator Plugin) (factory).
The following example renames the jobs ci.*_2
to ci.*
(it removes the trailing _2
).
[
[env: '01', hostname: 'host01.intranet'],
[env: '02', hostname: 'host02.intranet'],
[env: '03', hostname: 'host03.intranet'],
[env: '04', hostname: 'host04.intranet'],
[env: '05', hostname: 'host05.intranet'],
[env: '06', hostname: 'host06.intranet'],
[env: '07', hostname: 'host07.intranet'],
[env: '08', hostname: 'host08.intranet'],
[env: '09', hostname: 'host09.intranet'],
[env: '10', hostname: 'host10.intranet'],
[env: '11', hostname: 'host11.intranet'],
[env: '12', hostname: 'host12.intranet'],
[env: '13', hostname: 'host13.intranet'],
[env: '14', hostname: 'host14.intranet'],
[env: '15', hostname: 'host15.intranet'],
[env: '16', hostname: 'host16.intranet'],
[env: '17', hostname: 'host17.intranet'],
[env: '18', hostname: 'host18.intranet'],
[env: '19', hostname: 'host19.intranet'],
[env: '20', hostname: 'host20.intranet'],
].each { Map config ->
job("ci.${config.env}") {
previousNames("ci.${config.env}_2")
description("Continuous Integration on host ${config.env}")
logRotator {
numToKeep(5)
daysToKeep(45)
}
label('build')
wrappers {
colorizeOutput('gnome-terminal')
}
}
}