Jenkins Pipeline currentBuild 持续时间总是返回 0 [英] Jenkins Pipeline currentBuild duration time returns always 0

查看:20
本文介绍了Jenkins Pipeline currentBuild 持续时间总是返回 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;
}

我的问题是:

  1. 为什么我不知道时长
  2. 是否有办法在管道中指定构建后"步骤?从我读到的内容来看,尝试捕捉应该是这样的

我的临时解决方案是使用:

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-02-19 更新,2.14 版本的 Pipeline Support API Plugin 修复了这个问题,请参阅 这个问题

无法找到有关 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.

所以回答你的问题:

  1. 持续时间字段仅在构建完成后才有效.
  2. 不,无法指定构建后"步骤.

话虽如此,我认为您的解决方法是一个很好的解决方案(可以将其包装在一个函数中并将其放入 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).

至于你的最后一个额外问题我认为 currentBuild.duration 应该足够聪明,可以给出不同的单位(?).如果您指的是格式良好的字符串,例如 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆