你如何在 Jenkins 中获得构建持续时间?
How do you obtain build duration time in Jenkins?
我正在使用 Jenkins 管道配置 Android 应用程序构建过程。
在构建的开始和结束时,一条消息被发送到 Slack 频道。 Jenkinsfile 的相关部分如下所示:
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/ ")
我还希望 Slack 通知包括构建到 运行 所花费的时间。是否可以在不添加任何外部插件的情况下做到这一点?
如果有一个环境变量保存这些信息就完美了,但是我找不到这样的变量。
因为这个 jenkins-pipeline 脚本在 Groovy 中,你可以简单地在上面使用 new Date()
。 message
参数中的 "Current time ${new Date()}"
这样的东西必须有效:
slackSend (channel: '#slack-test', color: 'warning', message: "Current time ${new Date()}")
这将在您的频道中产生以下消息:
Current time: Thu Oct 13 17:25:12 CEST 2016
如果你想要一个特定的日期格式,你可以使用 format(String format)
方法,例如 "${new Date().format('dd/MM/yyyy')}"
:
slackSend (channel: '#slack-test', color: 'warning', message: "Current time ${new Date().format('dd/MM/yyyy')}")
这将产生以下消息:
Current time: 13/10/2016
更新
由于您不想使用任何外部插件,因此有一种可能的方法(这有点棘手),它是在您的 jenkins-pipeline 中使用以下脚本将开始时间保存在文件中:
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}"
然后在构建完成的另一个 jenkins-pipeline 中,解析文件中的日期并获取与当前时间的差异:
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}"
您可以使用 ${currentBuild.durationString}
获取构建持续时间。我在我的声明性管道脚本中使用它。
注意:如果您正在使用 HipChat plugin,您可以在 [=14] 中使用 ${BUILD_DURATION}
(以前是 ${DURATION}
)变量=] 命令(它由插件与其他一些变量一起传播)。
示例:
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>.'
}
}
这里还有一些可以在作业配置中使用的info on Jenkins environment variables。
您可以使用 ${currentBuild.durationString}
将其格式化为人类可读的格式(n 分钟 n 秒)。
但是它会以 and counting
为前缀,这有点奇怪。
所以我关注了this
${currentBuild.durationString.replace(' and counting', '')}
要从字符串中删除 'and counting' 部分,您可以执行以下操作:
"${currentBuild.durationString.minus(' and counting')}"
我正在使用 Jenkins 管道配置 Android 应用程序构建过程。
在构建的开始和结束时,一条消息被发送到 Slack 频道。 Jenkinsfile 的相关部分如下所示:
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/ ")
我还希望 Slack 通知包括构建到 运行 所花费的时间。是否可以在不添加任何外部插件的情况下做到这一点?
如果有一个环境变量保存这些信息就完美了,但是我找不到这样的变量。
因为这个 jenkins-pipeline 脚本在 Groovy 中,你可以简单地在上面使用 new Date()
。 message
参数中的 "Current time ${new Date()}"
这样的东西必须有效:
slackSend (channel: '#slack-test', color: 'warning', message: "Current time ${new Date()}")
这将在您的频道中产生以下消息:
Current time: Thu Oct 13 17:25:12 CEST 2016
如果你想要一个特定的日期格式,你可以使用 format(String format)
方法,例如 "${new Date().format('dd/MM/yyyy')}"
:
slackSend (channel: '#slack-test', color: 'warning', message: "Current time ${new Date().format('dd/MM/yyyy')}")
这将产生以下消息:
Current time: 13/10/2016
更新
由于您不想使用任何外部插件,因此有一种可能的方法(这有点棘手),它是在您的 jenkins-pipeline 中使用以下脚本将开始时间保存在文件中:
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}"
然后在构建完成的另一个 jenkins-pipeline 中,解析文件中的日期并获取与当前时间的差异:
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}"
您可以使用 ${currentBuild.durationString}
获取构建持续时间。我在我的声明性管道脚本中使用它。
注意:如果您正在使用 HipChat plugin,您可以在 [=14] 中使用 ${BUILD_DURATION}
(以前是 ${DURATION}
)变量=] 命令(它由插件与其他一些变量一起传播)。
示例:
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>.'
}
}
这里还有一些可以在作业配置中使用的info on Jenkins environment variables。
您可以使用 ${currentBuild.durationString}
将其格式化为人类可读的格式(n 分钟 n 秒)。
但是它会以 and counting
为前缀,这有点奇怪。
所以我关注了this
${currentBuild.durationString.replace(' and counting', '')}
要从字符串中删除 'and counting' 部分,您可以执行以下操作:
"${currentBuild.durationString.minus(' and counting')}"