Getting current timestamp in inline pipeline script using pipeline plugin of hudson

前端 未结 5 844
天命终不由人
天命终不由人 2021-01-01 17:02

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:

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-01 17:44

    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.

提交回复
热议问题