Jenkins 管道 currentBuild 持续时间 returns 始终为 0
Jenkins Pipeline currentBuild duration time returns always 0
我正在尝试获取我们报告的构建持续时间,但它总是 returns 0.
通过阅读文档、浏览 Slack 插件源代码和阅读其他资源,我应该能够执行以下操作之一:
def duration = currentBuild.duration
def duration = currentBuild.durationString
def duration = currentBuild.durationString()
def duration = currentBuild.getDurationString()
none 其中有效。据我了解,这可能是因为我在构建实际完成之前调用它,因此持续时间尚不可用。
管道结构如下所示:
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;
}
我的问题是:
- 为什么我得不到时长
- 管道中是否有指定 "after build" 步骤的方法?据我所读,try-catching 应该是这样工作的
我的临时解决方案是使用:
int jobDuration = (System.currentTimeMillis() - currentBuild.startTimeInMillis)/1000;
效果很好,但总是以秒为单位给出时间,我 认为 currentBuild.duration
应该足够聪明,可以给出不同的单位 (?)
更新 2018-02-19,此问题已通过 2.14 版本的管道支持 API 插件修复,请参阅 this issue
无法找到任何有关 duration
预计何时有效的文档。但是判断 from the implementation 似乎是在 run/build 完成后直接设置的。我猜它在 currentBuild 对象上可用,因为它是用于表示 currentBuild.previousBuild 的同一个对象,可能已经完成。
所以回答你的问题:
- 持续时间字段仅在构建完成后有效。
- 不,无法指定 "after build" 步骤。
话虽如此,我认为您的解决方法是一个很好的解决方案(可以将其包装在一个函数中并将其放入 GPL(全球 Public 库)。
至于你最后的加分问题I think the currentBuild.duration should be smart enough to give different units (?)
。如果您指的是格式良好的字符串,例如 Took 10min 5sec
,currentBuild.duration
不会给您任何良好的格式,因为它只是 return 一个长值,其中包含已经过去的秒数。相反,您可以调用 hudson.Util#getTimeSpanString(long duration)
。像这样:
import hudson.Util;
...
echo "Took ${Util.getTimeSpanString(System.currentTimeMillis() - currentBuild.startTimeInMillis)}"
这将 return 具有当前构建持续时间的格式良好的字符串。
我正在尝试获取我们报告的构建持续时间,但它总是 returns 0.
通过阅读文档、浏览 Slack 插件源代码和阅读其他资源,我应该能够执行以下操作之一:
def duration = currentBuild.duration
def duration = currentBuild.durationString
def duration = currentBuild.durationString()
def duration = currentBuild.getDurationString()
none 其中有效。据我了解,这可能是因为我在构建实际完成之前调用它,因此持续时间尚不可用。
管道结构如下所示:
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;
}
我的问题是:
- 为什么我得不到时长
- 管道中是否有指定 "after build" 步骤的方法?据我所读,try-catching 应该是这样工作的
我的临时解决方案是使用:
int jobDuration = (System.currentTimeMillis() - currentBuild.startTimeInMillis)/1000;
效果很好,但总是以秒为单位给出时间,我 认为 currentBuild.duration
应该足够聪明,可以给出不同的单位 (?)
更新 2018-02-19,此问题已通过 2.14 版本的管道支持 API 插件修复,请参阅 this issue
无法找到任何有关 duration
预计何时有效的文档。但是判断 from the implementation 似乎是在 run/build 完成后直接设置的。我猜它在 currentBuild 对象上可用,因为它是用于表示 currentBuild.previousBuild 的同一个对象,可能已经完成。
所以回答你的问题:
- 持续时间字段仅在构建完成后有效。
- 不,无法指定 "after build" 步骤。
话虽如此,我认为您的解决方法是一个很好的解决方案(可以将其包装在一个函数中并将其放入 GPL(全球 Public 库)。
至于你最后的加分问题I think the currentBuild.duration should be smart enough to give different units (?)
。如果您指的是格式良好的字符串,例如 Took 10min 5sec
,currentBuild.duration
不会给您任何良好的格式,因为它只是 return 一个长值,其中包含已经过去的秒数。相反,您可以调用 hudson.Util#getTimeSpanString(long duration)
。像这样:
import hudson.Util;
...
echo "Took ${Util.getTimeSpanString(System.currentTimeMillis() - currentBuild.startTimeInMillis)}"
这将 return 具有当前构建持续时间的格式良好的字符串。