Jenkins自己支持豐富的API接口,經過調用接口,基本能夠實現全部須要的功能,包括獲取、觸發、刪除Job等。
Jenkins的Remote API以REST-like的形式進行提供,經過對特定的API執行相關請求便可。
能夠經過curl、wget和postman等工具模擬請求和調試。html
Jenkins API沒有統一的入口,而是採用…/api/
的REST API樣式,其中」…」 表示Jenkins資源的URL。
常見的Jenkins資源包括:站點(實例)、Job和Build。
也就是說,能夠經過不一樣級別的URL地址查看到當前的API信息,例如http://<IP:Port or URL>/api
查看當前站點全部相關的API說明。node
通常狀況下,都會支持以下三種訪問Jenkins API的方式python
Many objects of Jenkins provide the remote access API. They are available at /blueocean/.../api/ where "..." portion is the object for which you'd like to access. XML API Access data exposed in HTML as XML for machine consumption. Schema is also available. You can also specify optional XPath to control the fragment you'd like to obtain (but see below). For example, ../api/xml?xpath=/*/*[0]. For XPath that matches multiple nodes, you need to also specify the "wrapper" query parameter to specify the name of the root XML element to be create so that the resulting XML becomes well-formed. Similarly exclude query parameter can be used to exclude nodes that match the given XPath from the result. This is useful for trimming down the amount of data you fetch (but again see below). This query parameter can be specified multiple times. XPath filtering is powerful, and you can have it only return a very small data, but note that the server still has to build a full DOM of the raw data, which could cause a large memory spike. To avoid overloading the server, consider using the tree parameter, or use the xpath parameter in conjunction with the tree parameter. When used together, the result of the tree parameter filtering is built into DOM, then the XPath is applied to compute the final return value. In this way, you can often substantially reduce the size of DOM built in memory. JSON API Access the same data as JSON for JavaScript-based access. tree may be used. Python API Access the same data as Python for Python clients. This can be parsed into Python object as eval(urllib.urlopen("...").read()) and the resulting object tree is identical to that of JSON. However, when you do this, beware of the security implication. If you are connecting to a non-trusted Jenkins, the server can send you malicious Python programs. In Python 2.6 or later you can safely parse this output using ast.literal_eval(urllib.urlopen("...").read()) For more information about remote API in Jenkins, see the documentation.
- 站點支持的API: http://<Jenkins-Server-Address>/api - 查詢到站點中全部的job信息(JSON格式): http://<Jenkins-Server-Address>/api/json?pretty=true - 查詢到站點中全部的job信息(XML格式): http://<Jenkins-Server-Address>/api/xml - 經過tree進行過濾: http://<Jenkins-Server-Address>/api/json?pretty=true&tree=jobs[name[*]] - 重啓站點: http://<Jenkins-Server-Address>/restart - 安全重啓站點: http://<Jenkins-Server-Address>/safeRestart
- 當前的api說明: http://<Jenkins-Server-Address>/job/<Job-Name>/api/ - JSON格式: http://<Jenkins-Server-Address>/job/<Job-Name>/api/json?pretty=true - XML格式: http://<Jenkins-Server-Address>/job/<Job-Name>/api/xml
- 獲取Job的builds節點信息: http://<Jenkins-Server-Address>/job/<Job-Name>/api/json?pretty=true&tree=builds[*] - 獲取Job的builds節點下displayName節點信息: http://<Jenkins-Server-Address>/job/<Job-Name>/api/json?pretty=true&tree=builds[displayName] - 獲取Job的builds節點下指定displayName節點信息: http://<Jenkins-Server-Address>/job/<Job-Name>/api/json?pretty=true&tree=builds[displayName]{x,y} - 獲取兩個相關的節點信息,例如: http://<Jenkins-Server-Address>/job/<Job-Name>/api/json?pretty=true&tree=builds[displayName]{3,5},url[*]
- JSON格式: http://<Jenkins-Server-Address>/job/<Job-Name>/<Build-Number>/api/json?pretty=true - XML格式: http://<Jenkins-Server-Address>/job/<Job-Name>/<Build-Number>/api/xml - 獲取指定信息: http://<Jenkins-Server-Address>/job/<Job-Name>/<Build-Number>/api/json?pretty=true&&tree=<filter>
- 獲取最近的buildNumber: `curl --silent http://<Jenkins-Server-Address>/job/<Job-Name>/lastBuild/buildNumber` - 獲取最近穩定的buildNumber:`curl --silent http://<Jenkins-Server-Address>/job/<Job-Name>/lastStableBuild/buildNumber` - 獲取最近成功的buildNumber:`curl --silent http://<Jenkins-Server-Address>/job/<Job-Name>/lastSuccessfulBuild/buildNumber` - 獲取最近失敗的buildNumber:`curl --silent http://<Jenkins-Server-Address>/job/<Job-Name>/lastFailedBuild/buildNumber`
- 獲取(get方法)和更新(post方法)Job的description信息: http://<Jenkins-Server-Address>/job/<Job-Name>/description - 獲取(get方法)和更新(post方法)Job的詳細配置信息: http://<Jenkins-Server-Address>/job/<Job-Name>/config.xml - 不帶參數直接執行(post方法): http://<Jenkins-Server-Address>/job/<Job-Name>/build - 帶參數執行(post方法),例如: http://<Jenkins-Server-Address>/job/<Job-Name>/buildWithParameters?token=testuser\&AAA='test123'\&BBB='test789' - 禁用(post方法)指定的job: http://<Jenkins-Server-Address>/job/<Job-Name>/disable - 啓用(post方法)指定的job: http://<Jenkins-Server-Address>/job/<Job-Name>/enable - 刪除(post方法)指定的job: http://<Jenkins-Server-Address>/job/<Job-Name>/doDelete
經過API遠程觸發jobgit
在user的configure頁面,建立API token,而後記錄下token name對應的密文,保存設置並退出。
github
Job和所在project對匿名用戶開放build權限
json
這裏是經過curl命令segmentfault
# 方式-1:cookie crumb=$(curl -c cookies.txt -s 'https://<JENKINS_USER_ID>:1110e7943aeaa4316950dc423f5b15b114@<JENKINS_SERVER_URL>/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)') curl --cookie cookies.txt --user "<JENKINS_USER_ID>:1110e7943aeaa4316950dc423f5b15b114" -H $crumb -X POST -s https://<JENKINS_SERVER_URL>/job/REG-Lawson-CB-Approval-system/job/dev/job/test-remote/buildWithParameters?token=testuser # 方式-2:https dns crumb=$(curl -k -u "<JENKINS_USER_ID>:1110e7943aeaa4316950dc423f5b15b114" 'https://<JENKINS_SERVER_URL>/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)') curl -u "<JENKINS_USER_ID>:1110e7943aeaa4316950dc423f5b15b114" -H $crumb -X POST -s https://<JENKINS_SERVER_URL>/job/REG-Lawson-CB-Approval-system/job/dev/job/test-remote/buildWithParameters?token=testuser # 方式-3:https dns Parameters crumb=$(curl -k -u "<JENKINS_USER_ID>:1110e7943aeaa4316950dc423f5b15b114" 'https://<JENKINS_SERVER_URL>/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)') curl -u "<JENKINS_USER_ID>:1110e7943aeaa4316950dc423f5b15b114" -H $crumb -X POST -s https://<JENKINS_JOB_URL>/buildWithParameters?token=testuser\&TEST_ONE='test123'\&TEST_TWO='test789' # 方式-4:http ip:port crumb=$(curl -k -u "<JENKINS_USER_ID>:1110e7943aeaa4316950dc423f5b15b114" 'http://<IP:PORT>/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)') curl -u "<JENKINS_USER_ID>:1110e7943aeaa4316950dc423f5b15b114" -H $crumb -X POST -s http://<IP:PORT>/<JOB_PATH>/buildWithParameters?token=testuser
直接觸發
帶參數觸發
api