Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you obtain build duration time in Jenkins?

I'm configuring an Android application build process with Jenkins pipeline.

At the beginning and the end of the build, a message is sent to a Slack channel. The relevant portion of the Jenkinsfile looks like so:

slackSend (channel: '#slack-test', color: 'warning', message: "Finished: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' State: ${STATE}.   Artifacts can be viewed here: ${env.BUILD_URL}artifact/Product/build/outputs/ ")

I'd also like the Slack notification to include the time it took for the build to run. Is it possible to do this without adding any external plugins?

If there's an environment variable which holds this information it would be perfect, but I can't find such a variable.

like image 807
Itai Ganot Avatar asked Oct 13 '16 13:10

Itai Ganot


1 Answers

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.

like image 61
Vadim Kotov Avatar answered Sep 26 '22 00:09

Vadim Kotov