Set job start timeout in Gitlab CI
GitLab CI (continuous integration) is a part of the GitLab platform. It allows us to define and configure pipelines as code using a YAML file called .gitlab-ci.yml within our projects' repositories. These pipelines consist of one or more stages, and each stage can contain multiple jobs.
Explore CI (continuous integration) and CD (continuous deployment) pipelines in detail.
The timeout keyword in GitLab CI
In GitLab CI, the timeout keyword defines a maximum time limit for a job to run. If the job exceeds the fixed timeout value, GitLab CI will automatically terminate the job.
Note: This Answer assumes your prior familiarity with GitLab.
We have a sample project in d3.js which displays a force-directed graph on the screen. Suppose you're managing this project on GitLab. Your file hierarchy will look as follows:
image: node:14
stages:
- build
- test
- deploy
before_script:
- npm install
build:
stage: build
script:
- npm run build
test:
stage: test
script:
- npm run test
deploy:
stage: deploy
script:
- npm run deploy
environment:
name: production
You can follow the steps given below, to add a job start timeout.
Navigate to your
.gitlab-ci.ymlfile.Locate
build,test, anddeployjobs.Add
timeoutkeyword followed by a:. Then add the duration in which you want your project to time out.If the job exceeds its specified timeout, GitLab CI will automatically terminate the job and mark it failed.
image: node:14stages:- build- test- deploybefore_script:- npm installbuild:stage: buildscript:- npm run buildtimeout: 30 minutestest:stage: testscript:- npm run testtimeout: 1 hourdeploy:stage: deployscript:- npm run deployenvironment:name: productiontimeout: 2 hours
Code explanation
Line 1: The
imagekeyword specifies the Docker image to use for the CI/CD pipeline. In this case, it's set tonode:14, which means the pipeline will run in an environment based onNode.jsversion 14.Lines 3–4: The
stagessection defines the different stages of the CI/CD pipeline. In this example, we have three stages:build,test, anddeploy.Lines 8–9: The
before_scriptsection allows you to specify the commands that need to be executed before running the jobs in each stage. In this case, it runsnpm install, which installs the project dependencies before executing any other job in the pipeline.Lines 11–15: The
buildjob is defined in thebuildstage. It represents the process of building thed3.jsproject. Thescriptsection contains the commandnpm run build, which executes the build script defined in the project. Additionally, thetimeoutfield specifies a maximum duration of30 minutesfor the job to run. If the job exceeds this time limit, it will be terminated.Lines 17–21: The
testjob is defined in theteststage. It represents the process of running tests. Thescriptsection contains the commandnpm run test, which executes the test script defined in the project. Thetimeoutfield specifies a maximum duration of1 hourfor the job to run.Lines 23–29: The
deployjob represents the process of deploying the builtd3.jsproject, likely to a production environment. Thescriptsection contains the commandnpm run deploy, which executes the deploy script. Theenvironmentfield is used to define the deployment environment (set toproductionin this case). Lastly, thetimeoutfield specifies a maximum duration of2 hoursfor the job to run.
Conclusion
Choose an appropriate timeout duration based on the expected execution time of your job. It's worth noting that the timeout keyword is optional, and you can choose to omit it if you don't want to enforce any specific time limit on your jobs.
Free Resources