最近,我想更新Cloudbees中的一些作业(未使用DSL定义),为每个作业添加一些属性。
好吧,我在使其工作时遇到了一些麻烦,这是我的注意事项(我使用的是Jenkins 1.651.2.1,但有可能它应与较早和较新的版本一起使用,例如jenkins 2)
没有安全性/没有身份验证
这是简单的部分:检索并重新发布配置
$ curl http://localhost:8080/jenkins/job/pof/config.xml -o config.xml
$ curl -X POST http://localhost:8080/jenkins/job/pof/config.xml --data-binary @config.xml
简单的安全性:使用用户名和密码
我现在假设您的Jenkins设置已设置安全性( http:// localhost:8080 / jenkins / configureSecurity / –>启用安全性)
这意味着我们现在需要验证我们的两个请求:
curl -X GET http://anthony:anthony@localhost:8080/jenkins/job/pof/config.xml -o config.xml
curl -X POST http://anthony:anthony@localhost:8080/jenkins/job/pof/config.xml --data-binary "@config.xml"
简单的安全性:启用CSRF(滚动)
您还需要保护您的jenkins实例免受CSRF攻击 ( http:// localhost:8080 / jenkins / configureSecurity / –> enable csrf crumb)
现在,这也意味着您的请求需要发送一个屑状值,无论是作为参数还是通过标头
如果您不这样做:
curl -X POST http://anthony:anthony@localhost:8080/jenkins/job/pof/config.xml --data-binary "@config.xml"
您会得到这样的错误:
<body><h2>HTTP ERROR 403</h2>
<p>Problem accessing /jenkins/job/pof/config.xml. Reason:
<pre> No valid crumb was included in the request</pre></p><hr /><i><small>Powered by Jetty://</small></i><br/>
甚至 :
<body><h1>HTTP Status 500 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Exception report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The server encountered an internal error () that prevented it from fulfilling this request.</u></p><p><b>exception</b> <pre>java.io.IOException: Failed to persist config.xml
hudson.model.AbstractItem.updateByXml(AbstractItem.java:677)
hudson.model.AbstractItem.doConfigDotXml(AbstractItem.java:617)
…..
</pre></p><p><b>root cause</b> <pre>javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:755)
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:357)jenkins.util.xml.XMLUtils._transform(XMLUtils.java:96)jenkins.util.xml.XMLUtils.safeTransform(XMLUtils.java:63)hudson.model.AbstractItem.updateByXml(AbstractItem.java:674)hudson.model.AbstractItem.doConfigDotXml(AbstractItem.java:617)
获得面包屑值:
您可以使用configure job页面来分配值:
curl http://anthony:anthony@localhost:8080/jenkins/job/pof/configure | sed -n 's/.*\.crumb", "\(.*\)").*/\1/p' > crumb.txt
但是,还有专门用于此的服务:
curl http://anthony:anthony@localhost:8080/jenkins/crumbIssuer/api/xml | sed -n 's/.*\(.*\)<\/crumb>.*/\1/p' > crumb.txt
使用面包屑值
curl -X POST http://anthony:anthony@localhost:8080/jenkins/job/pof/config.xml --data-binary "@config.xml" -data ".crumb=6bbabc426436b72ec35e5ad4a4344687"
哎呀,那没用
Caused by: java.lang.IllegalStateException: STREAMEDat org.eclipse.jetty.server.Request.getReader(Request.java:803)at javax.servlet.ServletRequestWrapper.getReader(ServletRequestWrapper.java:256)at hudson.model.AbstractItem.doConfigDotXml(AbstractItem.java:610)
我建议您使用标题发送面包屑:
curl -v -X POST http://anthony:anthony@localhost:8080/jenkins/job/pof/config.xml --data-binary "@config.xml" -H ".crumb: 6bbabc426436b72ec35e5ad4a4344687"
基于cookie的安全性(无用户名/密码)
在某些安装中(例如cloubees),您不能在请求中传递用户名和密码。 我建议您改用cookie。
要检索它们,请检查通过身份验证的浏览器发送的cookie,例如chrome:
然后将此URL粘贴到您的shell中:
curl 'http://localhost:8080/jenkins/job/pof/config.xml' -H 'Pragma: no-cache' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-US,en;q=0.8,fr;q=0.6' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Referer: http://localhost:8080/jenkins/login?from=%2Fjenkins%2Fjob%2Fpof%2Fconfig.xml' -H 'Cookie: screenResolution=1440x900; JSESSIONID=XXXXX; JSESSIONID.XX=XXXX; screenResolution=1440x900' -H 'Connection: keep-alive' -H 'Cache-Control: no-cache' --compressed
当然,您仍然需要获取面包屑值:
curl 'http://localhost:8080/jenkins/crumbIssuer/api/xml' -H 'Pragma: no-cache' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-US,en;q=0.8,fr;q=0.6' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Referer: http://localhost:8080/jenkins/login?from=%2Fjenkins%2Fjob%2Fpof%2Fconfig.xml' -H 'Cookie: screenResolution=1440x900; JSESSIONID=XXXXX; JSESSIONID.XX=XXXXX; screenResolution=1440x900' -H 'Connection: keep-alive' -H 'Cache-Control: no-cache' --compressed | sed -n 's/.*<crumb>\(.*\)<\/crumb>.*/\1/p' > crumb.txt
现在,您可以发布更新的config.xml了:
curl -X POST 'http://localhost:8080/jenkins/job/pof/config.xml' -H 'Pragma: no-cache' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-US,en;q=0.8,fr;q=0.6' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Referer: http://localhost:8080/jenkins/login?from=%2Fjenkins%2Fjob%2Fpof%2Fconfig.xml' -H 'Cookie: screenResolution=1440x900; JSESSIONID=XXXX; JSESSIONID.XX=XXXX; screenResolution=1440x900' -H 'Connection: keep-alive' -H 'Cache-Control: no-cache' --compressed --data-binary "@config.xml" -H ".crumb: 6bbabc426436b72ec35e5ad4a4344687"
链接
- https://benkiew.wordpress.com/2012/01/12/automating-hudsonjenkins-via-rest-and-curl-a-very-small-cookbook/
- https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API
翻译自: https://www.javacodegeeks.com/2016/05/update-jenkins-job-posting-config-xml.html