36

I have tried to rename a hudson/jenkins job. However it failed to rename.

Is there any way so I can rename the job?

Exploring
  • 2,493
  • 11
  • 56
  • 97

8 Answers8

70

You can rename selected job through jenkins UI by following these steps:

job>configure>Advanced Project Options>Display Name

Other way is to rename the directory on the Jenkins/hudson server and then restart the Jenkins.

MangeshBiradar
  • 3,820
  • 1
  • 23
  • 41
  • 4
    You don't have to restart Jenkins to pick up a renamed job directory. You can Manage `Jenkins>Reload Configuration from Disk` or just hit http://your-jenkins-server/reload. Note that the if you change a job name you will need to manually change any other job that tries to call the renamed job. – Kevin Cross Apr 25 '13 at 07:38
  • 1
    I suspect that the solution given by @marc.guenther is more correct, as this appears to refer to the name that is displayed, and not the actual job name. – M. Justin Oct 05 '15 at 17:53
  • Be aware that the manual renaming of the job folder on file system level (same as moving jobs into a different folder via UI) will not update data folders of plugins using the job name as well, e.g. config history plugin – Florian Straub Jun 18 '19 at 07:20
  • This doesn't really rename the job though. It just changes what is displayed. – Daniel Stevens Aug 10 '21 at 16:57
23

For Quick understanding I used some screenshots:

I used Jenkins version: 2.148

Step 1:

On home screen / Job list just click on job option.

enter image description here

OR

Just open Job details you will see left side option for rename.

enter image description here

Step 2:

Enter new name and just click on Rename button

enter image description here

Rikin Patel
  • 8,848
  • 7
  • 70
  • 78
18

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.

artdanil
  • 4,952
  • 2
  • 32
  • 49
  • Does this rename the whole job or just replace MY_JOB with whatever new part you choose? – Fadi Dec 16 '15 at 16:28
  • 1
    In the given example I am just updating the job name with adding "_NEW" suffix to all the jobs that start with search string "MY_JOB". The method 'renameTo()' will set the job name to the string you provide as parameter. If you want to replace part of the job do that before calling renameTo() and then pass the new name string to the method. – artdanil Jul 25 '16 at 20:48
  • This being such an old example, only finds jobs listed at the top level, not inside folders. Note also that it really renames the job, so the URL changes. _Not just cosmetic._ – MarkHu Jan 15 '22 at 23:59
11

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.

marc.guenther
  • 2,761
  • 2
  • 23
  • 18
  • Note that the field label appears to vary based on the type of project, as mine says "Maven project name" and not "Project name". – M. Justin Oct 05 '15 at 18:09
  • "Renaming on filesystem level should not be necessary" -- well, if you have spaces in the directory name, that causes script issues. – JESii Oct 28 '17 at 11:13
  • Meanwhile (v2.179) the UI doesn't allow changing the project name anymore but there is the rename option as mentioned by Rikin Patel – Florian Straub Jun 21 '19 at 10:34
6
  1. Create a new job with new name, there will be an option of copy from an existing job.
  2. copy from the job you want to rename.
  3. Delete the original job.

Now you have an identical job with a different name.

Slam
  • 8,112
  • 1
  • 36
  • 44
aditi
  • 61
  • 1
  • 1
2

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())
    }
  }
}
Jirong Hu
  • 2,315
  • 8
  • 40
  • 63
  • Please attribute source: https://wiki.jenkins-ci.org/display/JENKINS/Bulk+rename+projects – Jayan Jul 26 '16 at 00:57
  • @Jayan clearly they are the author of that snippet too from the author field. – ti7 Dec 04 '17 at 23:13
  • NOTE: The `item.renameTo(newname)` line really does rename the job, i.e. the URL changes. _Not just cosmetic._ If you only want to set the `item.displayName` then do that instead. – MarkHu Jan 16 '22 at 00:21
1

Depending on the requirement I usually choose between:

Job > Configure - modify Project Name property - Advanced Project Options, hit Advanced..., set value for Display Name

Then Save the job. No need to rename on file system level.

MrsTang
  • 3,119
  • 1
  • 19
  • 24
  • Job is the selected jenkins job you want to rename, if you have admin permissions there is a *configure* link in the sidebar. – MrsTang Jan 30 '13 at 12:43
  • Yes I have admin permissions...and I can see `configure` link to the right sidebar, but inside the configuration wizard where is the `modify Project Name property - Advanced Project Options` –  Jan 30 '13 at 12:46
  • I am using Jenkins ver. 1.499 –  Jan 30 '13 at 12:47
  • The *Project Name* is the first field in the configuration screen. It is editable, when you click save you'll be asked if you want to change the project name. Click yes. For the second option... Scroll a little until you see the section **Advanced Project Options** - on the right side there is an *Advanced...* button that you need to click, then you see and can modify the display name. – MrsTang Jan 30 '13 at 13:12
0

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')
        }
    }
}
oHo
  • 51,447
  • 27
  • 165
  • 200