Jenkins 常用API
假设 jenkins 部署地址为: http://localhost:8080 11c2d2167ff4c9e1710a6ff86d344e2c9c
Jenkins RestAPI调用出现Error 403 No valid crumb was included in the request
-
方法一
在jenkins 的Configure Global Security下 , 取消“防止跨站点请求伪造(Prevent Cross Site Request Forgery exploits”的勾选, 但是这样可能会造成安全问题,不推荐。
-
方法二
- 获取用户API token(User 是 "admin")
http://Jenkins_IP:8080/user/admin/configure
添加 new API Token, 或者找到以前的Token
2、计算CRUMB
CRUMB=$(curl -s 'http://admin:API_TOKEN@Jenkins_IP:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')
3、请求时附带CRUMB信息即可
curl -X POST -H "$CRUMB" http://admin:API_TOKEN@Jenkins_IP:8080/createItem/api/json?name=xxx
Jenkins RestAPI
创建 Job POST http://localhost:8080/createItem?name=xxx
request body 需有config.xml
更新 Job POST http://localhost:8080/job/{job_name}/config.xml/api/json
获取 Job GET http://localhost:8080/job/{job_name}/api/json
获取 JobXml GET http://localhost:8080/job/{job_name}/config.xml/api/json
创建 Build POST http://localhost:8080/job/{job_name}/build/api/json
(常用)Build with Parameters POST http://localhost:8080/job/{job_name}/buildWithParameters?XX=XX
获取 Build信息 GET http://localhost:8080/job/test/{build_number}/api/json
(常用)获取 Build信息 GET http://localhost:8080/job/test/lastBuild/api/json
(常用)获取TXT日志 GET http://localhost:8080/job/test/{build_number}/logText/progressiveText/api/json
获取 Html 日志 GET http://localhost:8080/job/test/{build_number}/logText/progressiveHtml/api/json
通过RestAPI去build Job,然后根据build结果信息或者Console获取想要的信息。