Reputation: 159
I have a requirement in Jenkins wherein,
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.
Upvotes: 1
Views: 2531
Reputation: 805
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.
Upvotes: 2
Reputation: 9075
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()
}
}
Upvotes: 1