我是Jenkins中使用的Groovy脚本的新手。我的问题是,我们如何根据作业名称和内部版本号获取作业的时间戳?
示例 :我想获取“ MyDeployJob”(JobName)的时间戳,并获取BuildNumber-105的时间戳。
我尝试使用方法getBuildByBuildNumber()方法,但无法正常工作。另外,我发现BUILD_ID环境变量具有时间戳,但是我无法弄清楚如何使用Groovy Script获取细节。请帮助。
getBuildByBuildNumber()
BUILD_ID
提前致谢。
下面是在Jenkins控制台中使用Groovy如何获取Job,按Job编号构建以及与时间相关的所有示例:
//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全局变量参考:
currentBuild
echo currentBuild.durationString
您可以在http:// yourjenkinsurl / pipeline-syntax / globals中的某些字段下找到所有详细信息:
timeInMillis :自计划构建开始以来的时间 startTimeInMillis :自构建开始开始以来的时间 运行持续时间 :以毫秒为单位的构建 持续时间durationString :构建 持续时间 的人类可读表示形式