javascript调用jenkins rest api来创建job

本文简要介绍了如何使用JavaScript调用Jenkins的REST API来创建一个新的Job。创建Job时,需要POST `config.xml`到指定URL,并附带参数`name=JOBNAME`,设置`Content-Type: application/xml`头。成功创建会返回200状态码,失败则返回4xx或5xx状态码。`config.xml`是Jenkins在文件系统中存储项目配置的格式,可以通过查看Jenkins主目录中或现有Job的`config.xml`获取示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jenkins提供了好多rest api供我们调用,这里简单介绍下用javascript调用jenkins api创建一个job,声明下只是单纯创建一个job,什么配置也没有.

首先看jenkins create job rest api用法介绍:

Create Job

To create a new job, post config.xml to this URL with query parameter name=JOBNAME. You need to send a Content-Type: application/xml header. You will get a 200 status code if the creation is successful, or 4xx/5xx code if it fails. config.xml is the format Jenkins uses to store the project in the file system, so you can see examples of them in the Jenkins home directory, or by retrieving the XML configuration of existing jobs from /job/JOBNAME/config.xml.

所以ajax post内容如下:

$.ajax({
	url: http://localhost:8080/createItem?name=tast1,
	async:false,
	type: post,
	dataType: 'json',
	data: '<?xml version="1.0" encoding="UTF-8"?><project><keepDependencies>false</keepDependencies><properties/><scm class="hudson.scm.NullSCM"/><canRoam>true</canRoam><disabled>false</disabled><blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding><blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding><triggers/><concurrentBuild>false</concurrentBuild><builders/><publishers/><buildWrappers/></project>'
}

其中data就是创建一个job需要的xml文件内容,这里创建一个job名字为test1,关于这个xml文件内容怎么获取的,本人采取的方法是先手动创建一个job,   通过如下地址可打开其xml,
域名地址 /job/JOBNAME/config.xml 
再copy过来的,至于里面细节内容还没来得及研究.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值