How to display the time it takes to start a build in Jenkins?

I customize the build process for Android applications using the Jenkins pipeline.

At the beginning and at the end of the assembly, the message is sent to the corresponding Slack channel.

The relevant part of the Jenkins file looks like this:

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 would also like to indicate the time it took for the assembly for this message, which notifies the completion of the assembly.

Can I do without adding external plugins? if there is an environment variable that contains this information, it would be ideal, but I cannot find such a variable.

+7
source share
3 answers

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

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

This will result in the following message in your channel:

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

If you need a specific date format, you can use the 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')}") 

Instead, the following message will be displayed:

 Current time: 13/10/2016 

UPDATE

Since you don't want to use any external plugins for a possible way to do this (a bit more complicated), to save the start time in a file using the following script in your jenkins project:

 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 another jenkins project where the build ends, analyze 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
source

You can use ${currentBuild.durationString} to get the build time. I use it in my declarative pipeline scripts.

Note : if you use the HipChat plugin , you can use the ${BUILD_DURATION} variable (formerly ${DURATION} ) in your hipchatSend (it is distributed 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 information on Jenkins environment variables that you can use when setting up your work.

+5
source

You can use ${currentBuild.durationString} to format it in a human-readable format (n minutes and seconds). However, the prefix and counting will be added to it, which is a bit strange.

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

0
source

Source: https://habr.com/ru/post/1258146/


All Articles