Jenkins Pipeline currentBuild持续时间始终返回0 [英] Jenkins Pipeline currentBuild duration time returns always 0
问题描述
我正在尝试获取报告的构建持续时间,但它始终返回0.
I am trying to get build duration for our report but it always returns 0.
通过阅读文档,浏览Slack插件源以及阅读其他资源,我应该能够执行以下操作之一:
From reading docs, going through Slack plugin source and reading other resources I should be able to do one of the following:
def duration = currentBuild.duration
def duration = currentBuild.durationString
def duration = currentBuild.durationString()
def duration = currentBuild.getDurationString()
均无效.据我了解,这可能是因为我在构建实际完成之前就调用了它,因此持续时间尚不可用.
none of which works. From my understanding this might be because I am calling this before the build actually finished and so th duration is not available yet.
管道的结构如下所示:
node {
try {
stage("Stage 1"){}
stage("Stage 2"){}
} catch (e) {
currentBuild.result = "FAILED"
throw e
} finally {
notifyBuild(currentBuild.result)
}
}
def notifyBuild(String buildStatus = 'STARTED') {
def duration = currentBuild.duration;
}
我的问题是:
- 为什么我没有得到持续时间
- 管道中是否有一种方法可以指定构建后"步骤?从我读到的内容来看,try-catching应该可以这样工作
我的临时解决方案是使用:
My temporary solution is to use:
int jobDuration = (System.currentTimeMillis() - currentBuild.startTimeInMillis)/1000;
哪个可以正常工作,但总是以秒为单位,我认为 currentBuild.duration
应该足够聪明,可以给出不同的单位(?)
Which works fine, but always gives time in seconds and I think the currentBuild.duration
should be smart enough to give different units (?)
推荐答案
更新2018年2月19日,此问题已在2.14版本的管道支持API插件中得到修复,请参见
Update 2018-02-19, this was fixed with 2.14 release of Pipeline Support API Plugin, see this issue
无法找到有关何时预期duration
有效的任何文档.但是从实现中判断 似乎是在运行/构建完成后直接设置的.我猜测它在currentBuild对象上可用,因为它与用于表示currentBuild.previousBuild的对象相同,可能已经完成.
In unable to find any documentation about when duration
is expected to be valid. But judging from the implementation it seems like it's set directly after the run/build completes. I'm guessing that it is available on the currentBuild object since it's the same object that is used for representing currentBuild.previousBuild, which may have completed.
所以回答您的问题:
- 持续时间字段仅在构建完成后才有效.
- 否,无法指定构建后"步骤.
话虽如此,我认为您的解决方法是一个很好的解决方案(可以将其包装在一个函数中,然后将其放入GPL(全球公共图书馆)中.
With that said, I think your workaround is a good solution (may be wrap it in a function and put it in an GPL (Global Public Library).
关于您的最后奖金问题I think the currentBuild.duration should be smart enough to give different units (?)
.如果您指的是格式良好的字符串,例如Took 10min 5sec
,则currentBuild.duration
不会给您任何格式,因为它只是返回一个长的值以及已过去的秒数.相反,您可以做的是调用 hudson.Util#getTimeSpanString(long duration)
.像这样:
As for your final bonus question I think the currentBuild.duration should be smart enough to give different units (?)
. If you are referring to the nicely formatted string, like Took 10min 5sec
, currentBuild.duration
won't give you any nice formatting since it simply returns a long value with the number of seconds that has elapsed. Instead, what you can do is call hudson.Util#getTimeSpanString(long duration)
. Like this:
import hudson.Util;
...
echo "Took ${Util.getTimeSpanString(System.currentTimeMillis() - currentBuild.startTimeInMillis)}"
这将返回格式正确且具有当前构建持续时间的字符串.
This will return a nicely formatted string with the current build duration.
这篇关于Jenkins Pipeline currentBuild持续时间始终返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!