使用 hudson 的管道插件获取内联管道脚本中的当前时间戳
Getting current timestamp in inline pipeline script using pipeline plugin of hudson
我想使用 hudson 的管道插件在内联管道脚本中获取当前时间戳。用于设置构建显示名称。
内联 groovy 使用的脚本:
def jobName = env.JOB_NAME + "_" + new Date()
currentBuild.displayName = "$jobName"
node {
echo "job name $jobName"
}
控制台错误:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:
Scripts not permitted to use new java.util.Date
Jenkins 脚本 运行 在沙箱中,默认情况下 Groovy 脚本没有某些操作的权限。
当您在没有权限的情况下执行操作时,将抛出 RejectAccessException
。所以你必须执行你的脚本,然后当抛出异常时去:
http://yourHost/jenkins/scriptApproval/
并批准必要的权限:
你也可以用这个,我在 ms 中需要这个所以:
echo "TimeStamp: ${currentBuild.startTimeInMillis}"
echo "TimeStamp: ${Util.getTimeSpanString(System.currentTimeMillis())}"
只需格式化 Date
对象:
stage('Foo') {
steps {
script {
def now = new Date()
println now.format("yyMMdd.HHmm", TimeZone.getTimeZone('UTC'))
}
}
}
您还可以通过在字符串上下文中使用 LocalDateTime
或 LocalDate
来避免脚本批准。这些将为您提供 ISO 8601 默认值:
script {
DATE_TAG = java.time.LocalDate.now()
DATETIME_TAG = java.time.LocalDateTime.now()
}
sh "echo ${DATETIME_TAG}"
有很多获取时间的方法,具体取决于您认为最直观的 API:
new Date()
has since been added to the script-security-plugin
whitelist
RunWrapper
APIs 通过使用 currentBuild
全局变量
final long startTime = currentBuild.startTimeInMillis
: long
构建开始时的值(以毫秒为单位)
final long scheduledTime = currentBuild.timeInMillis
:long
构建时间的毫秒值
final long buildDuration = currentBuild.duration
:构建花费的毫秒数
final String buildDurationAsStrong = currentBuild.durationString
: duration
作为 String
Using whitelisted java.time
APIs, for example LocalDateTime
import java.time.LocalDateTime
final LocalDateTime currentTime = LocalDateTime.now()
// do stuff with LocalDateTime
当然,在脚本中使用 return 值
final String currentTime = sh(returnStdout: true, script: 'date +%Y-%m-%d').trim()
我相信还有其他方法。
这是在您的本地时区中打印时间戳的简短方法:
String.format('%tF %<tH:%<tM', java.time.LocalDateTime.now())
我用它来 set build name and description, with a groovy token,例如:
${GROOVY,script = "String.format('%tF %<tH:%<tM', java.time.LocalDateTime.now())"}
打印我的当地时间:2021-12-05 16:07
请注意 build.getTimestampString2() 也会打印时间戳,但 为 UTC(可能与您的时区不同)。
我想使用 hudson 的管道插件在内联管道脚本中获取当前时间戳。用于设置构建显示名称。
内联 groovy 使用的脚本:
def jobName = env.JOB_NAME + "_" + new Date()
currentBuild.displayName = "$jobName"
node {
echo "job name $jobName"
}
控制台错误:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:
Scripts not permitted to use new java.util.Date
Jenkins 脚本 运行 在沙箱中,默认情况下 Groovy 脚本没有某些操作的权限。
当您在没有权限的情况下执行操作时,将抛出 RejectAccessException
。所以你必须执行你的脚本,然后当抛出异常时去:
http://yourHost/jenkins/scriptApproval/
并批准必要的权限:
你也可以用这个,我在 ms 中需要这个所以:
echo "TimeStamp: ${currentBuild.startTimeInMillis}"
echo "TimeStamp: ${Util.getTimeSpanString(System.currentTimeMillis())}"
只需格式化 Date
对象:
stage('Foo') {
steps {
script {
def now = new Date()
println now.format("yyMMdd.HHmm", TimeZone.getTimeZone('UTC'))
}
}
}
您还可以通过在字符串上下文中使用 LocalDateTime
或 LocalDate
来避免脚本批准。这些将为您提供 ISO 8601 默认值:
script {
DATE_TAG = java.time.LocalDate.now()
DATETIME_TAG = java.time.LocalDateTime.now()
}
sh "echo ${DATETIME_TAG}"
有很多获取时间的方法,具体取决于您认为最直观的 API:
new Date()
has since been added to thescript-security-plugin
whitelistRunWrapper
APIs 通过使用currentBuild
全局变量final long startTime = currentBuild.startTimeInMillis
:long
构建开始时的值(以毫秒为单位)final long scheduledTime = currentBuild.timeInMillis
:long
构建时间的毫秒值final long buildDuration = currentBuild.duration
:构建花费的毫秒数final String buildDurationAsStrong = currentBuild.durationString
:duration
作为String
Using whitelisted
java.time
APIs, for exampleLocalDateTime
import java.time.LocalDateTime final LocalDateTime currentTime = LocalDateTime.now() // do stuff with LocalDateTime
当然,在脚本中使用 return 值
final String currentTime = sh(returnStdout: true, script: 'date +%Y-%m-%d').trim()
我相信还有其他方法。
这是在您的本地时区中打印时间戳的简短方法:
String.format('%tF %<tH:%<tM', java.time.LocalDateTime.now())
我用它来 set build name and description, with a groovy token,例如:
${GROOVY,script = "String.format('%tF %<tH:%<tM', java.time.LocalDateTime.now())"}
打印我的当地时间:2021-12-05 16:07
请注意 build.getTimestampString2() 也会打印时间戳,但 为 UTC(可能与您的时区不同)。