詹金斯·格罗维(Jenkins Groovy)脚本 [英] Jenkins Groovy Scripting
问题描述
我是Jenkins中使用的Groovy脚本的新手.我的问题是,我们如何根据作业名称和内部版本号获取作业的时间戳?
I am new to Groovy scripting used in Jenkins. My question is that how do we fetch the timestamp of a job based on the job name and build number?
示例:我想获取"MyDeployJob"(JobName)的时间戳,并获取BuildNumber-105的时间戳.
Example: I want to get the timestamp of 'MyDeployJob' (JobName) and for the BuildNumber-105.
我尝试使用方法getBuildByBuildNumber()
方法,但无法正常工作.另外,我发现BUILD_ID
环境变量具有时间戳,但是我无法弄清楚如何使用Groovy Script来获取详细信息.请帮助.
I tried using the method getBuildByBuildNumber()
method, but its not working. 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. Kindly help.
谢谢.
推荐答案
在此示例中,如何获取工作,如何通过工作的编号进行构建以及在Jenkins控制台中使用Groovy进行的所有与时间有关的操作:
Here example how to get Job, build by number for the Job and all related to time using Groovy in Jenkins console:
//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))
输出:
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
要在管道中访问它们,可以使用currentBuild
全局变量参考:
To access them in pipeline, you can use currentBuild
Global Variable Reference:
echo currentBuild.durationString
您可以在下面的 http://yourjenkinsurl/pipeline-syntax/globals 中找到所有详细信息一些字段:
All details you can find in http://yourjenkinsurl/pipeline-syntax/globals, below some fields:
timeInMillis :自计划构建的时期以来的时间
startTimeInMillis :自开始构建以来的时间
运行持续时间:构建持续时间(以毫秒为单位)
durationString :构建持续时间的人类可读形式
timeInMillis : time since the epoch when the build was scheduled
startTimeInMillis : time since the epoch when the build started
running duration : duration of the build in milliseconds
durationString : a human-readable representation of the build duration
这篇关于詹金斯·格罗维(Jenkins Groovy)脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!