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


当前回答

简单的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); }

其他回答

如果你已经导出了config.xml文件,那么使用同样的文件导入:

curl -k -X POST 'https:///<user>:<token>@<jenkins_url>/createItem?name=<job_name>' --header "Content-Type: application/xml" -d @config.xml

我正在通过HTTPS连接,并使用-k禁用证书验证。

这是如何在Jenkins上生成用户api令牌。 如果单击Jenkins实例右下角的同名链接,就可以看到Jenkins REST API的详细信息。

简单的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); }

作业导入插件是这里从另一个Jenkins实例导入作业的简单方法。只需要提供源Jenkins实例的URL。Remote Jenkins URL可以接受以下任何类型的URL:

http://$JENKINS -获取远程实例上的所有作业 http://$JENKINS/job/$JOBNAME -找一份工作 http://$JENKINS/view/$VIEWNAME -获取特定视图中的所有作业

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

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

对于那些在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"