java Jenkins - groovy 脚本 - 以 dd-mm-yyyy 格式获取上次成功构建日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32180785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jenkins - groovy script - get last successful build date in dd-mm-yyyy format
提问by fabtrg
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.
我正在使用“groovy 脚本”插件作为我的 Jenkins 构建的一部分。我希望找到工作“RegularBuild”的最后一个成功构建日期,但是所有示例都在线例如
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.
不确定人们如何使用这种脚本语言,但基本原理失败了。更糟糕的是,我无法获得有效的错误评论,无论我输入什么,我得到的都是相同的错误,即插件根本没有帮助。
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
如果有人可以帮助使用正确的语法,甚至可以解决整个问题并提供上次成功构建的日期,那就太好了。最后一件事,请不要使用 xml 解决方案,因为 Jenkins 中没有任何东西可以将此值分配给环境变量,我知道。谢谢
回答by Jayan
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")
Edit
编辑
(From comments of @Steven the Easily Amused )
If the Jenkins uses folders , then you need getItemByFullName("/folder/yourjobname")
(来自@Steven the Easy Amused 的评论)如果 Jenkins 使用文件夹,那么您需要 getItemByFullName("/folder/yourjobname")
Edit 2
编辑 2
Fix date/time format s/MM:SS/mm:ss/ (substitute digits-of-month:milliseconds with minutes:seconds)
修复日期/时间格式 s/MM:SS/mm:ss/(用分钟:秒替换月数字:毫秒)