Jenkins REST API创建作业

nx7onnlm  于 2022-11-21  发布在  Jenkins
关注(0)|答案(2)|浏览(183)

我正在Jenkins中使用REST API创建一个新作业。

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"

错误:

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>
xurqigkl

xurqigkl1#

Jenkins默认情况下启用了CSRF保护,这会阻止one-click attacks。要调用请求,您需要使用凭据从/crumbIssuer/api/xml获取crumb,并将其包含在请求中。
例如:

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

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

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

如果以上方法不起作用,请检查您的crumb(echo $CRUMB)或使用-u USER:TOKEN运行curl
有关更详细的说明,请参阅:Running jenkins jobs via command line.

mo49yndu

mo49yndu2#

如果您使用Postman触发请求,使用上面的@kenorb示例,获取crumb x1c 0d1x

使用createItem终结点在根级别创建文件夹

使用createItem终结点在上面创建的文件夹中创建子文件夹

相关问题