Jenkins REST API 创建作业 [英] Jenkins REST API Create job

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

问题描述

我正在使用 REST API 在 Jenkins 中创建一个新作业.我尝试了下面的 curl 命令行,但它们抛出了一个错误

curl -i -X POST --user "admin:"--data-binary "@C:mylocalconfig.xml" -H "Content-Type: text/xml" http://localhost:8080/createItem?name=NewJobcurl -X POST -u <用户名>:-H "Content-Type:application/xml" -d "@C:mylocalconfig.xml" "http://localhost:8080/createItem?name=AA_TEST_JOB3"

错误:

<小时>

HTTP/1.1 403 请求中没有包含有效的面包屑日期:2016 年 7 月 1 日星期五 05:25:59 GMTX-Content-Type-Options: nosniff内容类型:文本/html;字符集=ISO-8859-1缓存控制:必须重新验证,无缓存,无存储内容长度:360服务器:Jetty(9.2.z-SNAPSHOT)<头><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><title>错误 403 请求中未包含有效的面包屑</title><body><h2>HTTP ERROR 403</h2><p>访问/createItem 时出现问题.原因:<预>请求中没有包含有效的面包屑


Power由 Jetty //</small></i><hr/>

<小时>

解决方案

Jenkins 默认有 CSRF 保护 已启用,可防止一键攻击一>.要调用请求,您需要使用您的凭据从 /crumbIssuer/api/xml 获取 crumb 并将其包含在您的请求中.

例如:

CRUMB=$(curl -s 'http://USER:TOKEN@localhost:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')

然后您可以创建一个作业(通过将面包屑包含在您的标题中):

curl -X POST -H "$CRUMB" "http://USER:TOKEN@localhost:8080/createItem?name=NewJob"

如果上述方法不起作用,请检查您的面包屑(echo $CRUMB)或使用 -u USER:TOKENcurl代码>.

更详细的解释参见:通过命令行运行 jenkins 作业.

I'm creating a new job in Jenkins using the REST API. I tried the below curl command lines, but they are throwing an error

curl -i -X POST --user "admin:<API token>" --data-binary "@C:mylocalconfig.xml" -H "Content-Type: text/xml" http://localhost:8080/createItem?name=NewJob

curl -X POST -u <username>:<pass> -H "Content-Type:application/xml" -d "@C:mylocalconfig.xml" "http://localhost:8080/createItem?name=AA_TEST_JOB3"

Error:


HTTP/1.1 403 No valid crumb was included in the request
Date: Fri, 01 Jul 2016 05:25:59 GMT
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=ISO-8859-1
Cache-Control: must-revalidate,no-cache,no-store
Content-Length: 360
Server: Jetty(9.2.z-SNAPSHOT)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Error 403 No valid crumb was included in the request</title>
</head>
<body><h2>HTTP ERROR 403</h2>
<p>Problem accessing /createItem. Reason:
<pre>    No valid crumb was included in the request</pre></p><hr><i><small>Power
ed by Jetty://</small></i><hr/>
</body>
</html>


解决方案

Jenkins by default has CSRF Protection enabled which prevents one-click attacks. To invoke the request, you need to obtain the crumb from /crumbIssuer/api/xml using your credentials and include it into your request.

For example:

CRUMB=$(curl -s 'http://USER:TOKEN@localhost:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')

Then you can create a job (by including the crumb into your header):

curl -X POST -H "$CRUMB" "http://USER:TOKEN@localhost:8080/createItem?name=NewJob"

If the above won't work, check your crumb (echo $CRUMB) or run curl with -u USER:TOKEN.

For a more detailed explanation, see: Running jenkins jobs via command line.

这篇关于Jenkins REST API 创建作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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