使用Groovy Jenkins API更新Jenkins作业 [英] Updating a Jenkins job with the Groovy Jenkins API

查看:608
本文介绍了使用Groovy Jenkins API更新Jenkins作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆