使用Groovy Jenkins API更新Jenkins作业 [英] Updating a Jenkins job with the Groovy Jenkins API
问题描述
我正在考虑使用Groovy脚本控制台在Jenkins上创建和更新作业.使用此处记录的API
I'm looking at using the Groovy script console to create and update jobs on Jenkins. Using the API documented here
http://javadoc.jenkins-ci.org/
我发现了如何使用创建工作
createProjectFromXML(String name, InputStream xml)
I've discovered how to create a job by using
createProjectFromXML(String name, InputStream xml)
但是,如果该作业已经存在,则此方法将失败.如何使用新的xml更新现有作业?
But this method will fail if the job already exists. How can I update an existing job with new xml?
更新
基于@ogondza的回答,我使用了以下内容来创建然后更新作业
Based on @ogondza's answer I used the follow to create and then update a job
import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
import java.io.*
import java.nio.charset.StandardCharsets
import javax.xml.transform.stream.*
config = """......My config.xml......"""
InputStream stream = new ByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8));
job = Jenkins.getInstance().getItemByFullName("job_name", AbstractItem)
if (job == null) {
println "Constructing job"
Jenkins.getInstance().createProjectFromXML("job_name", stream);
}
else {
println "Updating job"
job.updateByXml(new StreamSource(stream));
}
推荐答案
Use AbstractItem#updateByXml for updating. Also note that you can create/update jobs by XML using REST API and Jenkins CLI.
这篇关于使用Groovy Jenkins API更新Jenkins作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!