Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export build history of jenkins job

Tags:

java

jenkins

Can I export, in any conventional file format, a history of builds, with their time/date and success. And hopefully even promotion status.

like image 591
NimChimpsky Avatar asked Oct 18 '18 03:10

NimChimpsky


People also ask

How do I export a Jenkins job?

Export the jobStep 1- Open Jenkins and Go to the job which you want to export. Notes- We will use some commands which will help us to do our job. get-job- this will export the job in XML file. create-job – this will import the job from XML and will create job in Jenkins.

How do I download a report from Jenkins?

Export Report: Finalize Run Info: Specify a filename for the ZAP Report. Note that the file extension is not necessary. The report(s) will be saved into the Jenkins Job's Workspace. Info: Append the Build Variable BUILD_ID to the filename to ensure a quality name.

How do I get a list of all jobs in Jenkins?

Get a list of jobs. This can be done requesting http://jenkins_url:port/api/json?tree=jobs[name,url] .


1 Answers

You can make use of Jenkins rest api :

  1. Start at : Traverse all jobs on your Jenkins Server using :
    http://JENKINS_URl/api/json?tree=jobs[name,url]
    This will give json response with all jobs with job name and job url.
  2. Then for each job access its builds using api :
    http://JENKINS_URL/job/JOB_NAME/api/json?tree=allBuilds[number,url]
    This will give all the builds for job JOB_NAME and return json response with build number and build url.
  3. Now Traverse each build using api :
    http://JENKINS_URL/job/JOB_NAME/BUILD_NUMBER/api/json
    This will give everything related to the build as json response. Like Build status, how build was triggered, time etc.

For automation, you can use bash, curl and jq to achieve this.

Have written small bash script to retrieve build status and timestamp for each job on Jenkins server :

#!/bin/bash
JENKINS_URL=<YOUR JENKINS URL HERE>
for job in `curl -sg "$JENKINS_URL/api/json?tree=jobs[name,url]" | jq '.jobs[].name' -r`; 
do 
    echo "Job Name : $job"
    echo -e "Build Number\tBuild Status\tTimestamp"
    for build in `curl -sg "$JENKINS_URL/job/$job/api/json?tree=allBuilds[number]" | jq '.allBuilds[].number' -r`; 
    do 
        curl -sg "$JENKINS_URL/job/$job/$build/api/json" | jq '(.number|tostring) + "\t\t" + .result + "\t\t" + (.timestamp|tostring)' -r
    done 
    echo "================"
done

Note : Above script assumes that Jenkins server does not have any authentication. For authentication, add below parameter to each curl call :
-u username:API_TOKEN
Where :
username:API_TOKEN with your username and password/API_Token

Similar way you can export all build history in any format you want.

like image 81
Parvez Kazi Avatar answered Nov 15 '22 00:11

Parvez Kazi