jenkins定时任务
概述
JENKINS作为一款持续集成工具,还是比较简单易用的。
开发过程中,我们主要使用jenkins作为自动化编译工具和自动备份工具。
本文主要介绍一种常见场景的设置方法,即jenkins定时任务的设置方法。
环境
centos:CentOS release 7.0 (Final)或以上版本
Jenkins 2.332.2
定时任务配置
代码库使用本地gitlab管理,在jenkins中原本有一个任务,可以一键备份gitlab数据并归档管理。
该任务需要每周手动点击一次。但还是不够懒。
真的懒应该把这一次点击也省略掉。
打开任务的配置项,Dashboard -> Project Name -> 配置 -> 构建触发器 -> Build periodically,勾选Build periodically。
在输入框中输入定时任务配置项。
TZ=Asia/Shanghaieach saturday morning, run the jobH0 * * 6
上面的配置代表,在上海时间的每个星期六,0点H分启动1次定时任务,见截图。
配置说明
看了上面的配置方法,是不是觉得很熟悉,和linux系统的crontab定时任务很类似。
官方的使用说明也说了,和cron的规则很类似,但又有小小的不同。
其中最特别的一点,‘H‘代表’hash‘。
比如H 0 * * 6的配置表示,任务会在0点的0-59分随机选择分钟数来执行任务,这样的好处就是多个相似任务可以在执行时间上负载均衡,降低任务同时执行的服务器压力。
官方说明如下。
This field follows the syntax of cron (withminor differences). Specifically,eachline consistsof5fieldsseparatedbyTABorwhitespace:MINUTEHOURDOMMONTHDOWMINUTEMinuteswithinthehour(0–59)HOURThehouroftheday(0–23)
DOM Thedayofthemonth(1–31)MONTHThemonth(1–12)
DOW Thedayoftheweek(0–7)where0and7areSunday.Tospecify multiplevaluesforonefield, thefollowingoperatorsareavailable.Intheorderofprecedence,
* specifiesallvalidvaluesM-N specifies arangeofvaluesM-N/Xor*/X stepsbyintervalsofXthroughthe specifiedrangeorwhole validrangeA,B,...,Z enumerates multiplevaluesToallowperiodically scheduled taskstoproduce evenloadonthesystem, the symbol H (forhash) should be used wherever possible.Forexample,using00* * *fora dozen daily jobs will cause alargespikeatmidnight.Incontrast,usingH H * * * would stillexecuteeachjob once aday, butnotallatthe sametime, betterusinglimited resources.
The H symbol can be usedwitha range.Forexample, H H(0-7) * * * meanssometimebetween12:00AM (midnight)to7:59AM. You can alsousestep intervalswithH,withorwithoutranges.
The H symbol can be thoughtofasa randomvalueoverarange, but it actuallyisahashofthe jobname,nota randomfunction, so that thevalueremains stableforanygiven project.
Beware thatforthedayofmonthfield,shortcycles suchas*/3orH/3willnotworkconsistently near theendofmostmonths, duetovariablemonthlengths.Forexample, */3will runonthe1st,4th, …31stdaysofalongmonth,thenagain thenextdayofthenextmonth. Hashesarealwayschoseninthe1-28range, so H/3will produce a gapbetweenrunsofbetween3and6daysattheendofa month. (Longer cycles will also have inconsistent lengths but the effect may be relativelylessnoticeable.)Emptylinesandlinesthatstartwithwill be ignored as comments.Inaddition, @yearly, @annually, @monthly, @weekly, @daily, @midnight,and@hourlyaresupportedasconvenient aliases. Theseusethehashsystemforautomaticbalancing.Forexample, @hourlyisthe sameasH * * * *andcould meanatanytimeduring the hour. @midnight actually meanssometimebetween12:00AMand2:59AM.
Examples:Every fifteen minutes (perhaps at :07, :22, :37, :52):H/15* * * *Every ten minutes in the first half of every hour (three times, perhaps at :04, :14, :24):H(0-29)/10* * * *Once every two hours at 45 minutes past the hour starting at 9:45 AM and finishing at 3:45 PM every weekday:459-16/2* *1-5Once in every two hour slot between 8 AM and 4 PM every weekday (perhaps at 9:38 AM, 11:38 AM, 1:38 PM, 3:38 PM):H H(8-15)/2* *1-5Once a day on the 1st and 15th of every month except December:H H1,151-11*Timezone specification
Periodic tasksarenormally executedatthe scheduledtimeinthetimezoneofthe JenkinsmasterJVM (currently Asia/Shanghai). This behavior canoptionallybechangedbyspecifying an alternativetimezoneinthefirstlineofthe field.Timezone specification startswithTZ=, followedbytheIDofatimezone.Completeexampleofa schedulewithatimezone specification:
TZ=Europe/LondonThis job needs to be run in the morning, London timeH8* * *Butlers do not have a five oclock, so we run the job againH(0-30)17* * *
The supportedtimezones dependontheJavaruntime Jenkinsisrunning on. Thelistofsupportedtimezone IDsonthisinstanceis:
Africa/Abidjan
Africa/Accra
…
总结
Jenkins的功能很强大,在开发中是很好的辅助工具。
空空如常
求真得真
项目提交到github,然后Jenkins通过github的变化拉取代码进行maven构建,并把构建好的项目Jar包制作成image上传到doker或者阿里云仓库中,k8s集群再从镜像仓库中拉取镜像进行部署运行,整个过程全是自动化的,这个过程就是CICD的过程。