简体   繁体   中英

Jenkins Groovy Scripting

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?

Example : I want to get the timestamp of 'MyDeployJob' (JobName) and for the BuildNumber-105.

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.

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:

//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:

echo currentBuild.durationString

All details you can find in http://yourjenkinsurl/pipeline-syntax/globals , below some fields:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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