如何更新Jenkins作业发布config.xml
最近,我想更新Cloudbees中的一些作業(yè)(未使用DSL定義),為每個作業(yè)添加一些屬性。
好吧,我在使其工作時遇到了一些麻煩,這是我的注意事項(我使用的是Jenkins 1.651.2.1,但有可能它應與較早和較新的版本一起使用,例如jenkins 2)
沒有安全性/沒有身份驗證
這是簡單的部分:檢索并重新發(fā)布配置
$ 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)
現在,這也意味著您的請求需要發(fā)送一個屑狀值,無論是作為參數還是通過標頭
如果您不這樣做:
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)我建議您使用標題發(fā)送面包屑:
curl -v -X POST http://anthony:anthony@localhost:8080/jenkins/job/pof/config.xml --data-binary "@config.xml" -H ".crumb: 6bbabc426436b72ec35e5ad4a4344687"基于cookie的安全性(無用戶名/密碼)
在某些安裝中(例如cloubees),您不能在請求中傳遞用戶名和密碼。 我建議您改用cookie。
要檢索它們,請檢查通過身份驗證的瀏覽器發(fā)送的cookie,例如chrome:
然后將此URL粘貼到您的shell中:
當然,您仍然需要獲取面包屑值:
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現在,您可以發(fā)布更新的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
總結
以上是生活随笔為你收集整理的如何更新Jenkins作业发布config.xml的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三星E3屏幕(三星e3屏幕怎么样)
- 下一篇: jenkins api_接触Jenkin