Here’s a basic GitLab CI/CD YAML (.gitlab-ci.yml) example that demonstrates the use of the default: configuration option, along with an explanation.
The default: keyword in GitLab CI/CD allows you to define a set of parameters that will be inherited by all jobs in your pipeline unless a job explicitly overrides them. This is very useful for reducing repetition and ensuring consistency across your jobs.
YAML
# .gitlab-ci.yml
default:
image: ruby:3.0 # Default Docker image for all jobs
tags:
- docker # Default tags for runners
interruptible: true # Default for allowing jobs to be cancelled if a newer pipeline starts
before_script:
- echo "This runs before every job by default."
- ruby -v
stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "This is the build job."
- echo "It uses the default image (ruby:3.0) and before_script."
- bundle install # Example command that might use ruby
test_job_specific_image:
stage: test
image: node:18 # This job overrides the default image
before_script: # This job overrides the default before_script
- echo "Custom before_script for test_job_specific_image."
- node -v
script:
- echo "This is the test job."
- echo "It uses a specific image (node:18) and its own before_script."
- npm install # Example command that might use node
test_job_uses_defaults:
stage: test
script:
- echo "This test job uses all defaults from the 'default:' section."
- echo "Running some ruby tests..."
# Example: rspec
deploy_job:
stage: deploy
interruptible: false # This job overrides the default interruptible setting
script:
- echo "This is the deploy job."
- echo "It uses the default image and before_script, but is not interruptible."
# Deployment commands here
Code language: PHP (php)
Explanation:
default:Section:- This top-level keyword defines default configurations for all jobs in the pipeline.
image: ruby:3.0: Specifies that, by default, all jobs will run using theruby:3.0Docker image. This means the environment for the job will have Ruby 3.0 available.tags: - docker: By default, jobs will look for GitLab Runners that have thedockertag. Runners use tags to pick up specific jobs.interruptible: true: Sets the default behavior for jobs. Iftrue, the job will be canceled if it’s pending or running when a newer pipeline for the same ref (e.g., branch or MR) starts.before_script:: This block contains commands that will be executed before thescriptsection of every job, unless a job defines its ownbefore_script.echo "This runs before every job by default."ruby -v: This will print the Ruby version, confirming the default image is in use.
stages:- Defines the order in which jobs will be executed. Here, jobs in the
buildstage run first, thentest, thendeploy.
- Defines the order in which jobs will be executed. Here, jobs in the
build_job:stage: build: Assigns this job to thebuildstage.script:: Contains the main commands for this job.- Behavior: This job does not define its own
image,tags,interruptible, orbefore_script. Therefore, it inherits all of them from thedefault:section. It will useruby:3.0, run on adockertagged runner, beinterruptible, and execute the defaultbefore_scriptcommands before its ownscriptcommands.
test_job_specific_image:stage: test: Assigns this job to theteststage.image: node:18: This job overrides the defaultimage. Instead ofruby:3.0, it will use thenode:18Docker image.before_script:: This job also overrides the defaultbefore_script. The commands defined here will run instead of the ones in thedefault:section.- Inherited: It will still inherit the
tags: [docker]andinterruptible: truefrom thedefault:section because it doesn’t specify its owntagsorinterruptiblevalue. script:: Main commands for this job.
test_job_uses_defaults:stage: test: Assigns this job to theteststage.- Behavior: Similar to
build_job, this job does not specify animage,tags,interruptible, orbefore_script. It will inherit all settings from thedefault:section.
deploy_job:stage: deploy: Assigns this job to thedeploystage.interruptible: false: This job overrides the defaultinterruptiblesetting. This is common for deployment jobs, as you typically don’t want them to be automatically canceled.- Inherited: It will inherit
image: ruby:3.0,tags: [docker], and thebefore_scriptfrom thedefault:section. script:: Main commands for this job.
Benefits of Using default:
- DRY (Don’t Repeat Yourself): Reduces redundancy by defining common configurations in one place.
- Consistency: Ensures that jobs share a common baseline configuration unless explicitly stated otherwise.
- Maintainability: Makes it easier to update common settings (like a base Docker image or a common setup script) as you only need to change it in the
default:section. - Readability: Can make individual job definitions cleaner and more focused on their specific tasks.
When a GitLab Runner processes this .gitlab-ci.yml file, it will first look at the default: section and then apply those settings to each job. If a job has its own definition for a parameter that’s also in default:, the job-specific definition takes precedence.