7

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?

Andrew Gray
  • 3,593
  • 3
  • 35
  • 62
Siddarth
  • 351
  • 2
  • 6
  • 20

2 Answers2

12

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.

mkobit
  • 43,979
  • 12
  • 156
  • 150
Andrew Gray
  • 3,593
  • 3
  • 35
  • 62
2

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
Jared Forth
  • 1,577
  • 6
  • 17
  • 32
Gaurav
  • 21
  • 5
  • i tried getting the duration with json API in a post build task action like this "duration=$(curl -g -u login:token--silent "$BUILD_URL/api/json?pretty=true&tree=duration" | jq -r '.duration')" but i always get 0 – Youssef Boudaya Aug 16 '21 at 09:24