有可能在两个不同的詹金斯之间交换工作吗?我正在寻找一种输出/输入工作的方法。


当前回答

这并不适用于现有的工作,但是有Jenkins工作生成器。

这允许我们将作业定义保存在yaml文件和git repo中,这是非常可移植的。

其他回答

多亏了Larry Cai的回答,我成功地创建了一个脚本来备份我所有的Jenkins作业。我创建了一个每周运行这个的作业。如果有人发现它有用,这里是:

#!/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

简单的php脚本为我工作。

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); }

对于那些在Windows世界中可能有或没有Bash可用的人,这里是我对Katu的PowerShell移植和Larry Cai的方法。希望它能帮助到别人。

##### 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"

下载插件名非常简单

作业导入插件

输入Remote Jenkins服务器的URL,它将自动导入作业

作为web用户,您可以通过转到作业配置历史记录导出XML。

我现在无法访问Jenkins正在运行的机器,我想把它作为备份导出。

至于以web用户身份导入xml,我还是想知道。