Jenkins - groovy 脚本 - 以 dd-mm-yyyy 格式获取上次成功构建日期 [英] Jenkins - groovy script - get last successful build date in dd-mm-yyyy format

查看:49
本文介绍了Jenkins - groovy 脚本 - 以 dd-mm-yyyy 格式获取上次成功构建日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用groovy script"插件作为我的 Jenkins 构建的一部分.我希望找到工作RegularBuild"的最后一个成功构建日期,但是所有示例都在线,例如

I'm using "groovy script" plugin as part of my Jenkins build. I wish to find the last successful build date of a job "RegularBuild" however all the examples online e.g.

import hudson.model.Build;
def buildA = build("jobA")
println(buildA.getProject().getLastSuccessfulBuild())

即使这看起来没问题,也不要编译.

don't compile even though this seems to be ok.

不确定人们如何使用这种脚本语言,但基本原理失败了.更糟糕的是,我无法获得有效的错误评论,无论我输入什么,我得到的都是相同的错误,即插件根本没有帮助.

Not sure how people are using this scripting language but the fundamentals fail. To add to the pain I'm unable to get a valid error comment, all I get is the same error whatever I enter, i.e. the plugin is not helpful at all.

如果有人可以帮助提供正确的语法,甚至解决整个问题并提供上次成功构建的日期,那就太好了.最后一件事,请不要使用 xml 解决方案,因为 Jenkins 中没有任何东西可以将此值分配给环境变量,我知道.谢谢

If anyone could help with the correct syntax or even solve the whole problem and provide the date of the last successful build that would be great. One last thing, no xml solutions please as there is nothing in Jenkins that can assign this value to an ENVIRONMENT VARIABLE, well that I know of. Thanks

推荐答案

以下示例将对您有所帮助.我通常发现设置插件开发环境并查看实际类型和检查文档

Following sample will help you. I generally find it useful to setup a plugin development environment and see the actual Types and check documentation

import jenkins.model.Jenkins

def item = Jenkins.instance.getItem("your-job-name")
def  f=item.getLastFailedBuild()

println f.getTime()



def  ff=item.getLastSuccessfulBuild()
println ff.getTime().format("YYYY-MMM-dd HH:mm:ss")
println ff.getTime().format("dd-MM-yyyy")

编辑

(来自@Steven the Easy Amused 的评论)如果詹金斯使用文件夹,那么你需要 getItemByFullName("/folder/yourjobname")

(From comments of @Steven the Easily Amused ) If the Jenkins uses folders , then you need getItemByFullName("/folder/yourjobname")

编辑 2

修正日期/时间格式 s/MM:SS/mm:ss/(用分:秒代替月数:毫秒)

Fix date/time format s/MM:SS/mm:ss/ (substitute digits-of-month:milliseconds with minutes:seconds)

这篇关于Jenkins - groovy 脚本 - 以 dd-mm-yyyy 格式获取上次成功构建日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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