How to display the time it took to run a build in Jenkins?

*爱你&永不变心* 提交于 2019-12-22 04:54:09

问题


I'm configuring a process of Android application build by using Jenkins pipeline.

At the beginning and the end of the build, a message is sent to a relevant 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/ ")

In addition to the above information I'd like to also include the time it took for the build to run to that message which notifies about the end of the build.

Is that possible to do 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.


回答1:


Since this jenkins-pipeline script is in Groovy you can simply use new Date() on it. Something like this "Current time ${new Date()}" on the message argument must work:

slackSend (channel: '#slack-test', color: 'warning', message: "Current time ${new Date()}")

This will produce the follow message in your channel:

Current time: Thu Oct 13 17:25:12 CEST 2016

If you want a specific date format you can use format(String format) method, for example "${new Date().format('dd/MM/yyyy')}":

slackSend (channel: '#slack-test', color: 'warning', message: "Current time ${new Date().format('dd/MM/yyyy')}")

This instead will produce the follow message:

Current time: 13/10/2016

UPDATE

Since you don't want to use any external plugins a possible way to do so (it's a little tricky) it's to save the start time in a file using the follow script in your jenkins-pipeline:

def f = new File("/tmp/buildStart.txt")
def start = new Date().format('dd/MM/yyyy HH:mm:ss')
f.text = start
slackSend color: 'red', message: "Build start at ${start}"

Then in an other jenkins-pipeline where your build finish, parse the date from the file and get the difference with the current time:

def f = new File("/tmp/buildStart.txt")
def startDate = new Date().parse('dd/MM/yyyy HH:mm:ss',f.text)
def endDate = new Date()
def tookTime = groovy.time.TimeCategory.minus(endDate,startDate).toString()
slackSend color: 'red', message: "Total time: ${tookTime}"



回答2:


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.




回答3:


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', '')}




回答4:


To remove the 'and counting' part from the string you can do the following:

"${currentBuild.durationString.minus(' and counting')}"



来源:https://stackoverflow.com/questions/40022313/how-to-display-the-time-it-took-to-run-a-build-in-jenkins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!