[英]Updating a Jenkins job with the Groovy Jenkins API
I'm looking at using the Groovy script console to create and update jobs on Jenkins. 我正在使用Groovy脚本控制台在Jenkins上创建和更新作业。 Using the API documented here
使用此处记录的API
http://javadoc.jenkins-ci.org/ http://javadoc.jenkins-ci.org/
I've discovered how to create a job by using createProjectFromXML(String name, InputStream xml)
我已经发现如何使用
createProjectFromXML(String name, InputStream xml)
创建作业
But this method will fail if the job already exists. 但是,如果作业已存在,则此方法将失败。 How can I update an existing job with new xml?
如何使用新的xml更新现有作业?
Update 更新
Based on @ogondza's answer I used the follow to create and then update a job 根据@ ogondza的回答,我使用了以下内容来创建并更新作业
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. 使用AbstractItem#updateByXml进行更新。 Also note that you can create/update jobs by XML using REST API and Jenkins CLI.
另请注意,您可以使用REST API和Jenkins CLI通过XML创建/更新作业。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.