How do you obtain build duration time in Jenkins?

You can use ${currentBuild.durationString} to get build duration. I'm using it in my declarative pipeline scripts.

Note: If you're using HipChat plugin, you can use ${BUILD_DURATION} (previously ${DURATION}) variable in your hipchatSend command (it is propagated by the plugin with some other variables).

Example:

post {
  success {
    hipchatSend color: 'GREEN', room: 'My room', failOnError: true, notify: false, 
      message: 'Build <a href=\'${BUILD_URL}\'>${JOB_DISPLAY_NAME} #${BUILD_NUMBER}</a> has been built. Took ${BUILD_DURATION}. See the <a href=\'${BUILD_URL}/console\'>output</a>.'
  }
}

Here is some more info on Jenkins environment variables that can be used in job configuration.


You can use ${currentBuild.durationString} to get it formatted in a human-readable format (n minutes n seconds). However it will be prefixed with and counting which is kinda weird.

So I followed this ${currentBuild.durationString.replace(' and counting', '')}