How Do I Set a Timeout in a Jenkins Pipeline?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how do i set a timeout in a jenkins pipeline [Music] many pipelines can take a while to run to completion however a poorly written pipeline can waste resources on a controller worse yet if there are many poorly written pipelines on a controller it could starve a controller of all resources in this video we're going to look at a couple of different ways that you can use timeouts within the jenkins pipeline to help make sure that you are not one of those contributors to wasting resources 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 in this video we're going to be using a sample repository the link to the sample repository is down in the description so let's take a look at jenkins file one and this jenkins file is based on one of the jeans files that was from the what is an input in jenkins video which is also linked down in the description or also up here and what we have here is an input and steps so let's see what this looks like when we run it right now we have no timeout set so let's go ahead and set up a job called jenkins example timeout now we'll say pipeline click ok let's change this to pipeline script from scm get paste in our url change our branch to main and then jenkinsfile-1 let's click on save now when we click on build now what we're going to see is that we have an input requested and if we don't do anything then this is going to sit here and wait and wait and wait there is no timeout by default for an input now fortunately the way that this jenkins file is written it is not wasting any of our executors so at this point we can safely go and stop this job because unless we answer it nothing happens so we'll go ahead and just stop the job now let's go take a look at jenkins file 2. so we take a look at jenkins file 2. we have the exact same pipeline except we have added in an options block within the stage to time out after 10 seconds so let's go and modify our job to use jenkinsfile2 okay click on that click on build now now if we go back over to build number two we have an input is requested and it's going to time out after 10 seconds and we can see that it's been rejected by system finish aborted so we take a look now here we've had both of these abort but instead of having to manually go and abort the job by using the timeout option we timed it out after 10 seconds and we can see that here even though it's really small we can see that it was paused for 10 seconds now let's go back over and take a look at jenkins file 3. so in jenkins file 3 what we're doing is we're adding in a different option block so we still have our option timeout down here under the stage at 10 seconds but now we're setting up an option block for seven seconds at the global level what do you think is going to happen well let's go and modify our job and see what really happens we click on configure change this to jinkins file three click on save click on build now what we're going to see here momentarily is now job run three is running and we see two timeouts set to expire one for seven seconds one for 10 seconds and then it was cancelled due to the timeout now you might be asking okay which timeout really took effect the shortest answer is that since the timeout value for global was set to a shorter amount than the stage the global amount wins so if we were to take a look at pipeline steps what we can see here is that an enforced time limit was set for seven seconds at the global level and therefore the input only had seven seconds to complete now let's go take a look at the last case if we go take a look at jenkins file 4. what we've done is we've modified our global timeout to be 30 seconds so greater than 10 seconds on the stage so we're going to give the whole pipeline 30 seconds to timeout but we're only going to give the stage 10 seconds to timeout so if we come back over here in fact let's go back and take a look at the number three run what we can see here is this pause for basically six seconds so six seven seconds in that ballpark not ten seconds let's modify our job and do jenkins file 4. click save click on build now if we take a look at build run 4 we've set the timeout to expire in 30 seconds that's the global level we've set the stage to expire in 10 seconds and that timed out for us before we could even finish up and type in our input so let's go and run this job one more time and as we start it up we'll go over to build number five we'll click on our input we'll say submit yes dave it ran on the agent because of how we have it written it says good morning dave then it cats out some information from our agent and then it's done so we were able to get the input in within 10 seconds but the rest of the job completed before the 30 second timeout expired what are some other ways to use timeout well as we've seen even if we've written our input steps just correctly to where we are not using any executors anywhere the job is still running and is taking up some resources on the controller so inputs should always have timeouts another reason to use a timeout is to be able to warn you of abnormal processing times let's assume for a moment you have a job and typically it runs in about five minutes so you go and set a global timeout on that job to maybe eight nine ten minutes maybe time and a half two times whatever your normal run time is to allow for some margin of error that way if the job doesn't complete in that eight nine or ten minutes the job will be aborted and then the job through a post action can send a notification an email a slack notification to you to let you know that that job failed and was outside your time tolerance levels now just because it ran longer doesn't mean that your timeout was actually bad your job that used to run in five minutes over time has now expanded to run in 10 minutes or maybe 11 minutes so at that point you would need to update your jenkins file to adjust that global timeout from whatever it was from five minutes ten minutes to something a little bit longer to be able to support the new actual runtimes however if that's not the case and really your job still only takes five minutes you're going to be able to act more quickly on that failure that timeout instead of waiting around for a notification the next morning when somebody says hey what happened with the belt 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: 119
Rating: undefined out of 5
Keywords: darin pope, jenkins, jenkins tutorial, jenkins build timeout, jenkins declarative pipeline input timeout, jenkins build timeout setting, jenkins input timeout, jenkins job timeout, jenkins pipeline timeout, jenkins pipeline timeout example, jenkins stage timeout, jenkins timeout, jenkins timeout example, jenkins timeout pipeline, jenkinsfile timeout, timeout jenkins pipeline
Id: OChOtpK0fUE
Channel Id: undefined
Length: 8min 21sec (501 seconds)
Published: Thu Dec 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.