32

I want to get Getting current timestamp in inline pipeline script using pipeline plugin of hudson. For setting up build display name.

Inline groovy script used:

def jobName = env.JOB_NAME + "_" + new Date()
currentBuild.displayName = "$jobName"
node {
   echo "job name $jobName"
}

Error on console :

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: 
  Scripts not permitted to use new java.util.Date
albciff
  • 18,112
  • 4
  • 64
  • 89
Goutham Nithyananda
  • 871
  • 2
  • 13
  • 25
  • Have you once searched for the RejectedAccessException? Refer to http://stackoverflow.com/a/39412951/6894050 – arasio Oct 26 '16 at 12:20

6 Answers6

36

you can also use this, I needed this in ms so:

echo "TimeStamp: ${currentBuild.startTimeInMillis}"

echo "TimeStamp: ${Util.getTimeSpanString(System.currentTimeMillis())}"
Camilo Silva
  • 8,283
  • 4
  • 41
  • 61
dsaydon
  • 4,421
  • 6
  • 48
  • 52
32

Just format the Date object:

stage('Foo') {
  steps {
    script {
        def now = new Date()
        println now.format("yyMMdd.HHmm", TimeZone.getTimeZone('UTC'))
    }
  } 
}
Camilo Silva
  • 8,283
  • 4
  • 41
  • 61
28

There are a bunch of ways to get time depending on what APIs you find most intuitive:

  1. new Date() has since been added to the script-security-plugin whitelist

  2. RunWrapper APIs through use of currentBuild global variable

    1. final long startTime = currentBuild.startTimeInMillis: long value of when the build was started in milliseconds
    2. final long scheduledTime = currentBuild.timeInMillis: long value of when the build was scheduled in milliseconds
    3. final long buildDuration = currentBuild.duration: milliseconds it has taken to build
    4. final String buildDurationAsStrong = currentBuild.durationString: duration as a String
  3. Using whitelisted java.time APIs, for example LocalDateTime

    import java.time.LocalDateTime
    final LocalDateTime currentTime = LocalDateTime.now()
    // do stuff with LocalDateTime
    
  4. 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.

mkobit
  • 43,979
  • 12
  • 156
  • 150
  • In my case, #4 (shelling out) was the go for the time and log tracking tasks as node servers occasionally were out of time sync even for few minutes. – imy Jun 26 '23 at 12:07
15

You can also avoid script approvals by using LocalDateTime or LocalDate in string context. These will give you ISO 8601 defaults:

script {
  DATE_TAG = java.time.LocalDate.now()
  DATETIME_TAG = java.time.LocalDateTime.now()
}
sh "echo ${DATETIME_TAG}"
Asgeir S. Nilsen
  • 1,137
  • 9
  • 13
10

Jenkins scripts are running in a sandbox, by default the Groovy script doesn't have permissions for some operations.

When you perform an operation without permissions the RejectAccessException is thrown. So you've to execute your script, and then when the exception is thrown go to:

http://yourHost/jenkins/scriptApproval/

And approve the necessary permission:

enter image description here

albciff
  • 18,112
  • 4
  • 64
  • 89
2

Here's a short way to print timestamp in your local timezone:

String.format('%tF %<tH:%<tM', java.time.LocalDateTime.now())

I'm using it to set build name and description, with a groovy token, for example:

${GROOVY,script = "String.format('%tF %<tH:%<tM', java.time.LocalDateTime.now())"}

Prints my local time: 2021-12-05 16:07

Note that build.getTimestampString2() would also print the timestamp, but in UTC (might be different than your timezone).

Noam Manos
  • 15,216
  • 3
  • 86
  • 85