简体   繁体   English

Jenkins Groovy 脚本

[英]Jenkins Groovy Scripting

I am new to Groovy scripting used in Jenkins.我是 Jenkins 中使用的 Groovy 脚本的新手。 My question is that how do we fetch the timestamp of a job based on the job name and build number?我的问题是我们如何根据作业名称和内部版本号获取作业的时间戳?

Example : I want to get the timestamp of 'MyDeployJob' (JobName) and for the BuildNumber-105.示例:我想获取“MyDeployJob”(JobName)和 BuildNumber-105 的时间戳。

I tried using the method getBuildByBuildNumber() method, but its not working.我尝试使用getBuildByBuildNumber()方法,但它不起作用。 In addition, I found out that the BUILD_ID environment variable has the timestamp, but I am not able to figure out how to fetch the details using Groovy Script.此外,我发现BUILD_ID环境变量具有时间戳,但我无法弄清楚如何使用 Groovy Script 获取详细信息。 Kindly help.请帮忙。

Thanks in advance.提前致谢。

Here example how to get Job, build by number for the Job and all related to time using Groovy in Jenkins console:下面是如何在 Jenkins 控制台中使用 Groovy 获取作业、按作业编号构建以及所有与时间相关的示例:

//job by name
def job = Jenkins.instance.getItem("job name")

//build number
def build = job.getBuildByNumber(44)

println "getTimestampString: " + build.getTimestampString()
println "getTimestampString2: " + build.getTimestampString2()
println "getStartTimeInMillis: " + build.getStartTimeInMillis()
println "getTime: " + build.getTime()
println "getTimeInMillis: " + build.getTimeInMillis()
println "getTimestamp: " + build.getTimestamp()

//end time
println "End time: " + new Date(((long)build.getStartTimeInMillis() + build.duration))

Output:输出:

getTimestampString: 11 days
getTimestampString2: 2019-02-18T09:04:19Z
getStartTimeInMillis: 1550480659394
getTime: Mon Feb 18 09:04:19 UTC 2019
getTimeInMillis: 1550480659392
getTimestamp: java.util.GregorianCalendar[time=1550480659392,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Etc/UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=1,WEEK_OF_YEAR=8,WEEK_OF_MONTH=4,DAY_OF_MONTH=18,DAY_OF_YEAR=49,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=9,HOUR_OF_DAY=9,MINUTE=4,SECOND=19,MILLISECOND=392,ZONE_OFFSET=0,DST_OFFSET=0]
End time: Mon Feb 18 09:11:17 UTC 2019

To access them in pipeline, you can use currentBuild Global Variable Reference:要在管道中访问它们,您可以使用currentBuild全局变量引用:

echo currentBuild.durationString

All details you can find in http://yourjenkinsurl/pipeline-syntax/globals , below some fields:您可以在http://yourjenkinsurl/pipeline-syntax/globals 中找到的所有详细信息,在某些字段下方:

timeInMillis : time since the epoch when the build was scheduled timeInMillis : 自计划构建的纪元以来的时间
startTimeInMillis : time since the epoch when the build started startTimeInMillis : 自构建开始以来的时间
running duration : duration of the build in milliseconds运行持续时间:以毫秒为单位的构建持续时间
durationString : a human-readable representation of the build duration durationString :构建持续时间的人类可读表示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM