How to Schedule a Jenkins Job to Run Every Hour

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how to schedule a jenkins job to run every hour [Music] when you're first starting out with jenkins you're probably focused on creating jobs that you're either going to run manually or that are triggered by a web hook but what happens if you have jobs that need to be scheduled to run at a certain time of day week month or even year in this video we're going to take a look at the different ways you can schedule jobs within jenkins here's today's starting point i have a jenkins lts controller version 2.303.3 attached to this controller i have a linux based agent down in the description of this video is a link to a gist that has all of the commands that we're running today and links to any supporting documentation to get started let's go ahead and create a very simple pipeline job so i'm going to call this test pipeline and just for our example we're going to use the tri-sample pipeline hello world so this is good enough to get started and i'm going to make one small change sh echo hello world so that will be our pipeline to start with we'll click it save it let's go into a build now and make sure that this runs and it's green and that's all we needed so it's using agent one which is our agent and echo hello world so that's where we need to start but next up we want to figure out how to do our scheduling so if we go ahead and click on pipeline syntax and click on declarative directive generator and then under the sample directive let's select triggers and from triggers what we want to do is select cron which gives us our build periodically and if we go ahead and take a look at the help for schedule what we're going to see is the different options that we have to schedule our job we can see that it's broken down by minute hour day of month month and day of week and this syntax looks very similar to any basic cron that you would see except there are a couple of things that are specific for jenkins so if we wanted to create a schedule for a job to run at midnight every day what we would want is a zero for the minute a zero for the hour and then we'll just say wild card for all the others and when we tab out of this field what we'll see is this would have a last run at midnight utc and it's scheduled to run next at midnight utc now you'll also notice there is a warning here and it states that you may want to consider spreading load evenly by using h0 instead of zero zero and what the h is is that it stands for hash and the reason why you might want to use h instead of just the zero is what would happen if you had a lot of jobs scheduled for exactly at midnight you might be putting a lot of extra load on your controller that you really don't want to do so in the case of setting h for minute and tabbing out then we can see here that this would have last run at 1243 utc and it's scheduled to run at 12 43 utc tomorrow that way we don't have a race condition of jobs running all at the exact same time on the controller also included inside of the scheduling are some aliases in fact there is an alias for midnight so if i say at midnight and tab out what we're going to see here is that this would have been scheduled to run at 2 43 now you might be saying to yourself okay well 243 isn't midnight if you were to look at what the alias definition is for midnight it's actually scheduled for sometime between midnight and through the end of two am so in our case it would be somewhere between zero zero zero zero and zero three zero zero if we were to go take a look at the help again for this we would also see there are aliases for yearly annually monthly weekly daily midnight and even hourly now up to this point we've been working with utc but i actually don't live in utc i live in eastern time or also known from an iso perspective as america new underscore york so how would i set a job up to help me think in my time zone well i can simply just set the time zone equal to my iso code america new underscore york and let's go ahead and no schedule so let's hit the enter key here and let's set it to h0 star star star and if we tab out what this means is this would have last run tuesday at 5 43 utc so at this point in time we are in standard time so i'm 5 hours behind utc so midnight eastern time is 5 a.m utc now let's talk about what this video is really about how do i schedule a job to run every hour as we've seen we can either set specific times within an hour or we can set our job up to say just let this job run anytime within this hour in some cases you may need that min precision but in other cases as long as it runs within the hour you don't care so in the case where i want to have my job run every hour at the top of the hour on the zero minute what i'm going to do is i'm going to say zero so that's going to be the minute and then for every hour so that's going to be star one so for the hour and the slash one gives me the ability to add in repeating so for every hour or for every two hours but in my case i want every hour and then i'm going to say star star star and if i hit tab then what's going to happen here we can see that it would have run at coordinated utc and scheduled to run at 3 pm coordinated utc so at this point it's going to run right at the top of the hour but what if i don't really care about the exact top of the hour well what i would do is i would go ahead and change my zero here to an h and then it's going to randomly pick sometime within that hour in my case it picks the 43rd minute so it would have run at 143 utc now it's scheduled to run at 243 utc just so you can see a concrete example what we're going to do is we're going to set up a job to run every minute now we've already seen here how you can schedule it to run every hour that's what we see right now on the screen but if we want to run every minute it's just going to be five asterisks and then it's going to say hey did you really mean every minute when you said that because in our case the answer is yes so what i'm going to do is i'm going to generate a declarative directive so now i have triggers in cron i'm going to copy that and let's go back over to our job and modify our job to include this cron so we'll just paste it right here in this nice little section that's already open for us cron and that but when we click save this job will not be scheduled and why is that well when we click save all that does is just save the job until the job runs once the schedule will not take effect how do i know that if i go back into configure and take a look at build triggers and look at build periodically it's unchecked so until the job runs and the change that occurred within the pipeline is applied then the cron is not configured so i'm going to click on save one more time and we're going to click on build now just once if we take a look at build two what we see here is echo hello world that's all good but if we go take a look at the configuration again now under build triggers we see the schedule is set up with what is defined inside of our pipeline and as we're waiting for this to run here's test oh and it already ran number three ran at 245 in this case and we can say it said echo hello world what if the options for schedule do not support the exact scenario that you want for example what if you cared about the seconds within our scheduling options the granularity only goes down to a minute so in that case you would need to use some other system that has the knowledge of seconds and then from that system you could either make a rest api call or use the jinkin cli to run the job based on that external job scheduler if you have any questions or comments you can reach out to us on twitter at cloudbees if this video was helpful to you give us a thumbs up and if you haven't subscribed to cloudbees tv yet why not take a moment click on that subscribe button and then ring that bell and you'll be notified anytime there's new content available on cloudbees tv thanks for watching and we will see you in the next video
Info
Channel: CloudBeesTV
Views: 14,585
Rating: undefined out of 5
Keywords: darin pope, jenkins, jenkins tutorial, jenkins cron, jenkins cron calculator, jenkins cron generator, jenkins cron trigger, cron job jenkins, jenkins cron every hour, schedule a job for every hour in jenkins, schedule jenkins job every hour, jenkinsfile cron, jenkins pipeline cron, jenkins cron expression generator, jenkins build periodically, jenkins cron syntax, jenkins cron format
Id: JhvVJtYFUm0
Channel Id: undefined
Length: 9min 48sec (588 seconds)
Published: Tue Nov 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.