Jenkins REST API 创建作业

标签 jenkins

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

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>
<小时/>

最佳答案

Jenkins 默认有 CSRF Protection启用可防止 one-click attacks 。要调用请求,您需要从/crumbIssuer/api/xml获取面包屑。使用您的凭据并将其包含到您的请求中。

例如:

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 ) 或运行 curl-u USER:TOKEN .

有关更详细的说明,请参阅: Running jenkins jobs via command line

关于Jenkins REST API 创建作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38137760/

相关文章:

java - 如何将自定义证书添加到 Jenkins 内部 JDK?

jenkins - 没有这样的 DSL 方法 `stages`

Jenkins 工作失踪

jenkins slave 以用户身份运行

svn - 如何在 Jenkins 中获取最新的 SVN 标签

powershell - 使用 Powershell 运行 Cake 脚本时,Jenkins 中出现未知命令错误

maven - docker maven 镜像的 Jenkins 管道脚本退出状态 -1

java - jenkins和android名称归档文件名

java - 并行 Sonar 扫描仪执行锁定错误

jenkins - 如何在 Jenkins 中为某些用户级别阻止某些作业?