1

I have a requirement in Jenkins wherein,

  1. We have, for example, 10 jobs in view ABC with specific configuration.
  2. Now, I need to create a new view in Jenkins XYZ and clone all the jobs in ABC with different name and change the configuration, for example SCM URL.

I came across a groovy script which might do this, but I am not much aware of groovy

https://wiki.jenkins-ci.org/display/JENKINS/Clone+all+projects+in+a+View

We create a new branches (new URL's) for every release. So the jobs under the view ABC needs to be copied into XYZ with different names and URL's updated. I do not want to waste time creating each job individually with different name and then add them to the view XYZ

Any help would be highly appreciated.

Jninja
  • 149
  • 1
  • 3
  • 13
  • 1
    What's the question? What doesn't that script do? – tim_yates Jun 16 '14 at 17:23
  • I am not familiar with groovy. So I was not able to understand the variables defined, for example, def str_view = "MyProduct_Release_1.0" def str_search = "Rel_1.0" def str_replace = "Rel_1.1".... – Jninja Jun 16 '14 at 18:11
  • What I want is create a new view (XYZ), copy jobs from existing view ABC to XYZ with different job name. Thats it. I believe this groovy script should do that, but not sure about the fields in the script. – Jninja Jun 16 '14 at 18:13
  • Just found out that the script is creating a new job from an existing job in the same view itself. The requirement is to create a new view and then copy the jobs to that view with different job name – Jninja Jun 16 '14 at 18:42

2 Answers2

2

You can use the Folders Plugin and you then do not need to change your job names or manage views.

To copy jobs from one Folder to another you can use the ssh cli interface like this.

ssh -l USERNAME MYjenkins:port copy-job /OLD/job1 /NEW/job1 ssh -l USERNAME MYjenkins:port copy-job /OLD/job2 /NEW/job2

NOTE: some ssh clients do not support ":port" and require a command like this instead:

ssh -l USERNAME -p PORT MYjenkins copy-job /OLD/job1 /NEW/job1

If not using folders, you can automate creation of new jobs and views from existing jobs like this:

ssh -l USERNAME MYjenkins:port create-view NEW-VIEW ssh -l USERNAME MYjenkins:port copy-job jobN NEW_jobN ssh -l USERNAME MYjenkins:port add-job-to-view NEW-VIEW NEW_jobN

Please see the Cloudbees CLI for information on how to configure user keys and how to determine and configure the correct port to use.

You can even copy Jobs from one Jenkins instance to another by retrieving the job's config.xml then creating a new job with that XML as follows:

ssh -l USERNAME MYJenkins:port get-job AJOB > AJOB.xml ssh -l USERNAME MYOTHERJenkins:port2 create-job AJOB < AJOB.xml

One last hint: try ssh -l USERNAME MYJenkins:port help or ssh -l USERNAME MYJenkins:port help create-job

To get some hints about syntax and available commands.

  • NOTE: When copying from one Jenkins to another, there are many other considerations, e.g. does the target jenkins have the same plugins, and pertinent configuration: items like credentials, environment variables, and the like. – Steven the Easily Amused Sep 29 '17 at 21:34
1

You can use the jenkins job dsl for this which also contains an interface to create views

You can iterate the jobs in your view with this

http://<your jenkins server>:<your jenkins port if its not on 80>/view/<your view>/api/json

then clone the jobs with job command

job{
  name 'new name'
  using 'original name'

  //other configuration 
}

This is all coded in groovy as a build step in a separate job. You could even have parameters to name the view and jobs or drive it out of a SCM


EDIT A nice question. I implemented it like this

view named templateView

jobs called templateJobA, templateJobB, etc

def templateJobsURL = new URL("http://jenkins-server:8080/view/templateView/api/json")
def templateJobs = new groovy.json.JsonSlurper().parse(templateJobsURL.newReader())
def newJobs = []

templateJobs.jobs.each { 
    def templateName = it.name
    def newName = templateName.replaceAll('template','new')
    job {
        name newName
        using templateName
    }
    newJobs.push(newName)
    println templateName + ' ' + newName
}

view(type: ListView) {
    name('new')
    description('All jobs for project A')
    jobs {
        newJobs.each{
           names (it)
           println 'view add ' + it
        }
    }
    columns {
        status()
        weather()
        name()
        lastSuccess()
        lastFailure()
        lastDuration()
        buildButton()
    }
}
KeepCalmAndCarryOn
  • 8,817
  • 2
  • 32
  • 47
  • I have 120 jobs under a view. I would like to be able to copy these jobs configurations under a different view with different job names when we have a new branch. Is there a way to automate this. One way would be to go to the jenkins home directory, copy jobs and then chnage the config.xml – Jninja Jul 01 '14 at 13:46
  • May I know what has to be defined here. I am confused: templateJobs.jobs.each { def templateName = it.name def newName = templateName.replaceAll('template','new') job { name newName using templateName @KeepCalmAndCarryOn – Jninja Jul 09 '14 at 18:01
  • Hey I was trying to use the option you provided but not getting through. Would you be able to help me out I am getting confused with the parameters defined. Please let me know where to define the following: – Jninja Jul 15 '14 at 19:16
  • Existing view- XYZ Jobs under XYZ--sample-1, foobar-1, fifa-1 – Jninja Jul 15 '14 at 19:18
  • New view required- ABC required jobs to be created from XYZ as sample-2, foobar-2 and fifa-3 Basically, 1 needs to be replaced with 2. – Jninja Jul 15 '14 at 19:19