gpt4 book ai didi

Jenkins Pipeline currentBuild 持续时间总是返回 0

转载 作者:行者123 更新时间:2023-12-03 18:30:08 60 4
gpt4 key购买 nike

我正在尝试为我们的报告获取构建持续时间,但它总是返回 0。

通过阅读文档、浏览 Slack 插件源和阅读其他资源,我应该能够执行以下操作之一:

def duration = currentBuild.duration
def duration = currentBuild.durationString
def duration = currentBuild.durationString()
def duration = currentBuild.getDurationString()

这些都不起作用。据我了解,这可能是因为我在构建实际完成之前调用它,因此持续时间尚不可用。

管道的结构如下所示:
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;
}

我的问题是:
  • 为什么我没有得到持续时间
  • 管道中有没有办法指定“构建后”步骤?从我读到的内容来看,尝试捕捉应该是这样工作的

  • 我的临时解决方案是使用:
    int jobDuration = (System.currentTimeMillis() - currentBuild.startTimeInMillis)/1000;

    效果很好,但总是以秒为单位给出时间,我认为 currentBuild.duration应该足够聪明,可以给出不同的单位(?)

    最佳答案

    更新 2018-02-19,此问题已在 2.14 版本的 Pipeline Support API 插件中得到修复,请参阅 this issue

    无法找到有关何时 duration 的任何文档预计有效。但是判断from the implementation似乎它是在运行/构建完成后直接设置的。我猜它在 currentBuild 对象上可用,因为它与用于表示 currentBuild.previousBuild 的对象相同,它可能已经完成。

    所以回答你的问题:

  • 持续时间字段仅在构建完成后才有效。
  • 不,没有办法指定“构建后”步骤。

  • 话虽如此,我认为您的解决方法是一个很好的解决方案(可能是将其包装在一个函数中并将其放入 GPL(全局公共(public)图书馆)中。

    至于你最后的奖金问题 I think the currentBuild.duration should be smart enough to give different units (?) .如果您指的是格式良好的字符串,例如 Took 10min 5sec , currentBuild.duration不会给你任何好的格式,因为它只是返回一个长值,其中包含经过的秒数。相反,您可以调用 hudson.Util#getTimeSpanString(long duration) .像这样:
    import hudson.Util;

    ...

    echo "Took ${Util.getTimeSpanString(System.currentTimeMillis() - currentBuild.startTimeInMillis)}"

    这将返回具有当前构建持续时间的格式良好的字符串。

    关于Jenkins Pipeline currentBuild 持续时间总是返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42087792/

    60 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com