We can use REST-like Remote Access API in Jenkins to post or retrieve information in XML or JSON format. General format of the URL is <Jenkins-URL>/<data-context>/api/xml|json.
Below are few examples:
1) Retrieve Build Result.
Below examples shows the build result in JSON format for job 'web-apps-build-pkgvalidator' and build number ''6'. Credential is passed using '--user <user-name>:<password>'
result is piped to 'jq' to pretty print.
Notes:
- 'jq' is jQuery library. For more info visit https://jquery.com
- Instead of using password in your Jenkins API call, you can use API token. https://stackoverflow.com/questions/45466090/how-to-get-the-api-token-for-jenkins provides some quick instruction for generating Jenkins API token associated with your user.
$> curl -X GET http://localhost:9090/job/web-apps-build-pkgvalidator/6/api/json --user ppoudel:<password> | jq
|
2) Below example shows use of /api/xml with 'xpath' to get just the build status from the build report.
$> curl -X GET http://localhost:9090/job/web-apps-build-pkgvalidator/6/api/xml?xpath=/*/result --user ppoudel:<password>
<result>SUCCESS</result>
|
3) Getting build status using /api/json. The following example shows retrieving job name, build number, build status and timestamp.
$> curl -X GET http://localhost:9090/job/web-apps-build-pkgvalidator/6/api/json?tree=fullDisplayName,number,result,timestamp --user ppoudel:<password>
|
4) Retrieving all jobs under certain view:
Note: here I'm piping the result through 'jq' and 'grep', which is optional.
curl -X GET http://localhost:9090/job/Web/job/mobile-apps/view/mobile-apps/api/json --user ppoudel:<password> | jq | grep name
"name": "mobile-apps-xyzmportal", |
5) Retrieving JUnit test summary report:
$> curl http://localhost:9090/job/web-apps-build-pkgvalidator/6/testReport/api/json?tree=failCount,skipCount,totalCount,urlName --user ppoudel:<password>
|
6) Below steps can be used to retrieve the SonarQube analysis result using Jenkins' remote REST like API.
6.1) Get the taskId from the build providing build number
Note: http://localhost:9090 is Jenkins server URL.
curl -X GET http://localhost:9090/job/web-apps-build-pkgvalidator_sonarqube/6/api/json --user ppoudel:<password> | jq | grep ceTaskId |
6.2) Get the analysisId using taskID:
Note: http://localhost:8000 is SonarQube URL.
$> curl -X GET http://localhost:8000/api/ce/task?id=AWfXjftAinfFqLzOhqqe --user ppoudel:<password> | jq | grep analysisId
|
6.3) Get the analysis report using analysisId:
$> curl -X GET http://localhost:8000/api/qualitygates/project_status?analysisId=AWfXjgNNVNzkngEjXoPD --user ppoudel:<password> | jq
|
7) If you need to get all the configured projects (paginated output) in SonarQube you can use the following URL:
Note: http://localhost:8000 is SonarQube URL.
# First page: |
8) Or get detail of a specific project:
Note: http://localhost:8000 is SonarQube URL.
$> http://localhost:8000/api/components/show?key=<project-key> |
For more information on Remote access API visit https://wiki.jenkins.io/display/JENKINS/Remote+access+API
You may be interested reading the following Jenkins related blog:
Jenkins Job Chain and How to Resolve Issue with Parameter Passing
Jenkins Pipeline - Few Troubleshooting Tips
Thanks for sharing such a good blog.
ReplyDeleteDevOps Training
DevOps Online Training
Thanks for sharing this :) it is very helpfull to me (and everybody else) :)
ReplyDeleteCool and that i have a swell give: How Long Does House Renovation Take renovate my house
ReplyDelete