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


当前回答

一行程序:

$ curl -s http://OLD_JENKINS/job/JOBNAME/config.xml | curl -X POST 'http://NEW_JENKINS/createItem?name=JOBNAME' --header "Content-Type: application/xml" -d @-

身份验证:

$ 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 @-

使用Crumb,如果CSRF是活动的(参见这里的详细信息):

获取面包屑:

$ 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)')

使用-H crumb涂抹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 @-

其他回答

2021年,进出口过程很痛苦!!

如果你有shell访问两个jenkins实例:旧的和新的,遵循以下步骤来执行成功的作业迁移:

穿着你的旧内裤

在你的旧Jenkins里找到Jenkins家。通常/var/lib/jenkins.如果你使用bitnami: /opt/bitnami/jenkins 在Jenkins家中,进入工作文件夹 您应该看到带有作业名称的文件夹。在这些文件夹中,您只需要config.xml 备份所有需要的作业。只有文件夹和它的config.xml。还有很多其他文件不是必需的。

在你的新詹金斯:

找到詹金斯家 将作业(以前的备份)复制到jobs文件夹 确保这些新文件夹的所有者是用户“jenkins”。如果不使用这个:chown jenkins:jenkins /var/lib/jenkins -R 重启詹金斯 解析:选D

根据赞成票和评论的数量,我可以考虑创建一个新插件的可能性:)

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

在web浏览器中访问:

http://[jenkinshost]/job/[jobname]/config.xml

只需将文件保存到磁盘即可。

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

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

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

一行程序:

$ curl -s http://OLD_JENKINS/job/JOBNAME/config.xml | curl -X POST 'http://NEW_JENKINS/createItem?name=JOBNAME' --header "Content-Type: application/xml" -d @-

身份验证:

$ 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 @-

使用Crumb,如果CSRF是活动的(参见这里的详细信息):

获取面包屑:

$ 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)')

使用-H crumb涂抹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 @-