Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Jenkins build job duration

Is it possible to get Jenkins build duration using scripts or using some inbuilt functionality. I tried with ${BUILD_DURATION} but it didn't work.

Build duration displayed in Jenkins UI

Can anyone please suggest?

like image 712
Siddarth Avatar asked Aug 10 '18 13:08

Siddarth


2 Answers

Here are a couple of options:

RESTFUL API

Use a language of your choice to consume Jenkins Restful API to get the json for the build and deserialise.

DECLARATIVE PIPELINE

Use ${currentBuild.durationString}.

The currentBuild object exposes a number of relevant attributes:

timeInMillis: 
    time since the epoch when the build was scheduled

startTimeInMillis
    time since the epoch when the build started running

duration
    duration of the build in milliseconds

durationString
    a human-readable representation of the build duration

Navigate to https://<your-jenkins-instance>/pipeline-syntax/globals#currentBuild for the complete list.

like image 139
Andrew Gray Avatar answered Oct 23 '22 18:10

Andrew Gray


I am Jenkins beginner & below Groovy script is an easy approach;

def item = Jenkins.instance.getItem("<Job_name>")

def last_job_duration = item.getLastBuild().getDurationString()

println last_job_duration
like image 31
Gaurav Avatar answered Oct 23 '22 17:10

Gaurav