Is it possible to exchange jobs between 2 different Jenkins'? I'm searching for a way to export/import jobs.
问题:
回答1:
Jenkins has a rather good wiki, albeit hard to read when you're new to CI software...
They offer a simple solution for moving jobs between servers
The trick probably was the need to reload config from the Jenkins Configuration Page.
回答2:
Probably use jenkins command line is another option, see https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+CLI
- create-job: Creates a new job by reading stdin as a configuration XML file.
- get-job: Dumps the job definition XML to stdout
So you can do
java -jar jenkins-cli.jar -s http://server get-job myjob > myjob.xml
java -jar jenkins-cli.jar -s http://server create-job newmyjob < myjob.xml
It works fine for me and I am used to store in inside my version control system
回答3:
A one-liner:
$ curl -s http://OLD_JENKINS/job/JOBNAME/config.xml | curl -X POST 'http://NEW_JENKINS/createItem?name=JOBNAME' --header "Content-Type: application/xml" -d @-
Authentication
$ curl -s http:///<USER>:<API_TOKEN>@OLD_JENKINS/job/JOBNAME/config.xml | curl -X POST 'http:///<USER>:<API_TOKEN>@NEW_JENKINS/createItem?name=JOBNAME' --header "Content-Type: application/xml" -d @-
With Crumb, if CSRF is active (see details here)
get crumb with:
$ CRUMB_OLD=$(curl -s 'http://<USER>:<API_TOKEN>@OLD_JENKINS/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')
$ CRUMB_NEW=$(curl -s 'http://<USER>:<API_TOKEN>@NEW_JENKINS/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')
apply crumb with -H CRUMB
:
$ curl -s -H $CRUMB_OLD http:///<USER>:<API_TOKEN>@OLD_JENKINS/job/JOBNAME/config.xml | curl -X POST -H $CRUMB_NEW 'http:///<USER>:<API_TOKEN>@NEW_JENKINS/createItem?name=JOBNAME' --header "Content-Type: application/xml" -d @-
回答4:
There's a plugin called Job Import Plugin that may be what you are looking for. I have used it. It does have issues with importing projects from a server that doesn't allow anonymous access.
For Completeness: If you have command line access to both, you can do the procedure already mentioned by Khez for Moving, Copying and Renaming Jenkins Jobs.
回答5:
Go to your Jenkins server's front page, click on REST API at the bottom of the page:
Create Job
To create a new job, post config.xml
to this URL with query parameter name=JOBNAME
. You need to send a Content-Type: application/xml
header. You'll get 200
status code if the creation is successful, or 4xx/5xx
code if it fails. config.xml
is the format Jenkins uses to store the project in the file system, so you can see examples of them in the Jenkins home directory, or by retrieving the XML configuration of existing jobs from /job/JOBNAME/config.xml
.
回答6:
In my Jenkins instance (version 1.548) the configuration file is at:
/var/lib/jenkins/jobs/-the-project-name-/config.xml
Owned by jenkins user and jenkins group with 644 permissions. Copying the file to and from here should work. I haven't tried changing it directly but have backed-up the config from this spot in case the project needs to be setup again.
回答7:
Job Import plugin is the easy way here to import jobs from another Jenkins instance. Just need to provide the URL of the source Jenkins instance. The Remote Jenkins URL can take any of the following types of URLs:
http://$JENKINS
- get all jobs on remote instancehttp://$JENKINS/job/$JOBNAME
- get a single jobhttp://$JENKINS/view/$VIEWNAME
- get all jobs in a particular view
回答8:
Thanks to Larry Cai's answer I managed to create a script to backup all my Jenkins jobs. I created a job that runs this every week. In case someone finds it useful, here it is:
#!/bin/bash
#IFS for jobs with spaces.
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for i in $(java -jar /run/jenkins/war/WEB-INF/jenkins-cli.jar -s http://server:8080/ list-jobs);
do
java -jar /run/jenkins/war/WEB-INF/jenkins-cli.jar -s http://server:8080/ get-job ${i} > ${i}.xml;
done
IFS=$SAVEIFS
mkdir deploy
tar cvfj "jenkins-jobs.tar.bz2" ./*.xml
回答9:
Jenkins export jobs to a directory
#! /bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
declare -i j=0
for i in $(java -jar jenkins-cli.jar -s http://server:8080/jenkins list-jobs --username **** --password ***);
do
let "j++";
echo $j;
if [ $j -gt 283 ] // If you have more jobs do it in chunks as it will terminate in the middle of the process. So Resume your job from where it ends.
then
java -jar jenkins-cli.jar -s http://lxvbmcbma:8080/jenkins get-job --username **** --password **** ${i} > ${i}.xml;
echo "done";
fi
done
Import jobs
for f in *.xml;
do
echo "Processing ${f%.*} file.."; //truncate the .xml extention and load the xml file for job creation
java -jar jenkins-cli.jar -s http://server:8080/jenkins create-job ${f%.*} < $f
done
回答10:
Simple php script worked for me.
Export:
// add all job codes in the array
$jobs = array("job1", "job2", "job3");
foreach ($jobs as $value)
{
fwrite(STDOUT, $value. " \n") or die("Unable to open file!");
$path = "http://server1:8080/jenkins/job/".$value."/config.xml";
$myfile = fopen($value.".xml", "w");
fwrite($myfile, file_get_contents($path));
fclose($myfile);
}
Import:
<?php
// add all job codes in the array
$jobs = array("job1", "job2", "job3");
foreach ($arr as $value)
{
fwrite(STDOUT, $value. " \n") or die("Unable to open file!");
$cmd = "java -jar jenkins-cli.jar -s http://server2:8080/jenkins/ create-job ".$value." < ".$value.".xml";
echo exec($cmd);
}
回答11:
This does not work for existing jobs, however there is Jenkins job builder.
This allows one to keep job definitions in yaml files and in a git repo which is very portable.
回答12:
For those of us in the Windows world who may or may not have Bash available, here's my PowerShell port of Katu and Larry Cai's approach. Hope it helps someone.
##### Config vars #####
$serverUri = 'http://localhost:8080/' # URI of your Jenkins server
$jenkinsCli = 'C:\Program Files (x86)\Jenkins\war\WEB-INF\jenkins-cli.jar' # Path to jenkins-cli.jar on your machine
$destFolder = 'C:\Jenkins Backup\' # Output folder (will be created if it doesn't exist)
$destFile = 'jenkins-jobs.zip' # Output filename (will be overwritten if it exists)
########################
$work = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName())
New-Item -ItemType Directory -Force -Path $work | Out-Null # Suppress output noise
echo "Created a temp working folder: $work"
$jobs = (java -jar $jenkinsCli -s $serverUri list-jobs)
echo "Found $($jobs.Length) existing jobs: [$jobs]"
foreach ($j in $jobs)
{
$outfile = Join-Path $work "$j.xml"
java -jar $jenkinsCli -s $serverUri get-job $j | Out-File $outfile
}
echo "Saved $($jobs.Length) jobs to temp XML files"
New-Item -ItemType Directory -Force -Path $destFolder | Out-Null # Suppress output noise
echo "Found (or created) $destFolder folder"
$destPath = Join-Path $destFolder $destFile
Get-ChildItem $work -Filter *.xml |
Write-Zip -Level 9 -OutputPath $destPath -FlattenPaths |
Out-Null # Suppress output noise
echo "Copied $($jobs.Length) jobs to $destPath"
Remove-Item $work -Recurse -Force
echo "Removed temp working folder"
回答13:
It is very easy just download plugin name
Job Import Plugin
Enter the URL of your Remote Jenkins server and it will import the jobs automatically
回答14:
Importing Jobs Manually: Alternate way
Upload the Jobs on to Git (Version Control) Basically upload config.xml of the Job.
If Linux Servers:
cd /var/lib/jenkins/jobs/<Job name>
Download the config.xml from Git
Restart the Jenkins
回答15:
The most easy way, with direct access to the machine is to copy the job folder from first jenkins to another one (you can exclude workspaces), because the whole job configuration is stored in the xml file on the disk.
Then in the new jenkins just reload configuration in the global settings (admin access is required).
Another way can be to use plugins mentioned above this post.
回答16:
By default, this is set to ~/.jenkins, but you can change this in one of the following ways:
* Set "JENKINS_HOME" environment variable to the new home directory before launching
the servlet container.
* Set "JENKINS_HOME" system property to the servlet container.
* Set JNDI environment entry "JENKINS_HOME" to the new directory.
See the container specific documentation collection for more about how to do this for your container.
You can change this location after you've used Jenkins for a while, too. To do this, stop Jenkins completely, move the contents from old JENKINS_HOME to the new home, set the new JENKINS_HOME, and restart Jenkins.
JENKINS_HOME has a fairly obvious directory structure that looks like the following:
JENKINS_HOME
+- config.xml (jenkins root configuration)
+- *.xml (other site-wide configuration files)
+- userContent (files in this directory will be served under your
http://server/userContent/)
+- fingerprints (stores fingerprint records)
+- plugins (stores plugins)
+- workspace (working directory for the version control system)
+- [JOBNAME] (sub directory for each job)
+- jobs
+- [JOBNAME] (sub directory for each job)
+- config.xml (job configuration file)
+- latest (symbolic link to the last successful build)
+- builds
+- [BUILD_ID] (for each build)
+- build.xml (build result summary)
+- log (log file)
+- changelog.xml (change log)
Back up and restore:
All the settings, build logs, artifact archives are stored under the JENKINS_HOME directory. Simply archive this directory to make a back up. Similarly, restoring the data is just replacing the contents of the JENKINS_HOME directory from a back up.
Back ups can be taken without stopping the server, but when you restore, please do stop the server.
Moving/copying/renaming jobs
You can:
- Move a job from one installation of Jenkins to another by simply copying the corresponding job directory.
- Make a copy of an existing job by making a clone of a job directory by a different name.
- Rename an existing job by renaming a directory. Note that the if you change a job name you will need to change any other job that tries to call the renamed job.
Those operations can be done even when Jenkins is running. For changes like these to take effect, you have to click "reload config" to force Jenkins to reload configuration from the disk.
Batch renaming jobs
Replacing spaces in job names with underscores
$ rename 's/\s/_/g' *
Archive unused jobs
Sometimes you want to remove a job from Jenkins but do so in such a way that you can resurrect it later, if the need arises. You can do this by going to $JENKINS_HOME and create an archive of the job directory. The following command illustrates how to archive a job 'xyz' and remove it.
$ cd $JENKINS_HOME/jobs
$ tar czf xyz.tgz xyz
// go to Jenkins GUI "Manage Jenkins" page and "Reload Configuration from Disk"
As long as you are not building the xyz project while you create an archive, you can do this operation without taking Jenkins offline.
See also the "Shelve Project" plugin.
Script Console
Useful for trouble-shooting, diagnostics or batch updates of jobs Jenkins provides a script console which gives you access to all Jenkins internals. These scripts are written in Groovy and you'll find some samples of them in this page.
URL Options
http://[jenkins-server]/[command]
where [command] can be
- exit shutdown jenkins
- restart restart jenkins
- reload to reload the configuration