Getting current timestamp in inline pipeline script using pipeline plugin of hudson
you can also use this, I needed this in ms so:
echo "TimeStamp: ${currentBuild.startTimeInMillis}"
echo "TimeStamp: ${Util.getTimeSpanString(System.currentTimeMillis())}"
Just format the Date
object:
stage('Foo') {
steps {
script {
def now = new Date()
println now.format("yyMMdd.HHmm", TimeZone.getTimeZone('UTC'))
}
}
}
There are a bunch of ways to get time depending on what APIs you find most intuitive:
new Date()
has since been added to thescript-security-plugin
whitelistRunWrapper
APIs through use ofcurrentBuild
global variablefinal long startTime = currentBuild.startTimeInMillis
:long
value of when the build was started in millisecondsfinal long scheduledTime = currentBuild.timeInMillis
:long
value of when the build was scheduled in millisecondsfinal long buildDuration = currentBuild.duration
: milliseconds it has taken to buildfinal String buildDurationAsStrong = currentBuild.durationString
:duration
as aString
Using whitelisted
java.time
APIs, for exampleLocalDateTime
import java.time.LocalDateTime final LocalDateTime currentTime = LocalDateTime.now() // do stuff with LocalDateTime
Of course, shelling out and using the return value in your script
final String currentTime = sh(returnStdout: true, script: 'date +%Y-%m-%d').trim()
And I'm sure there are other methods, too.