GitLab CI/CD
Configuration
The .gitlab-ci.yml
file defines the CI/CD configuration for a project. This file is located in the root of the repository and is used to define pipelines, jobs, and stages.
Example .gitlab-ci.yml
:
stages:
- build
- test
- deploy
build:
stage: build
script:
- echo "Building the project..."
image: ruby:2.7
test:
stage: test
script:
- echo "Running tests..."
image: ruby:2.7
deploy:
stage: deploy
script:
- echo "Deploying to production..."
image: ruby:2.7
only:
- master
Options:
- stages: Defines the stages of the pipeline. Each stage must be executed in order.
- image: Specifies the Docker image to use for the job.
- script: Defines the commands to execute for the job.
- only: Specifies the branches or tags on which the job should run.
Source: https://docs.gitlab.com/ee/ci/yaml/README.html
Runners
Runners are the agents that execute the jobs defined in the .gitlab-ci.yml
file. Runners can be either shared or specific to a project.
Types of runners:
- Shared runners: Provided by GitLab and can be used by any project.
- Specific runners: Registered to a specific project and can only be used by that project.
Source: https://docs.gitlab.com/ee/ci/runners/README.html
Jobs
Jobs are the individual tasks that are executed within a pipeline. Each job is defined in the .gitlab-ci.yml
file and can be configured with its own settings, such as the image, script, and dependencies.
Example job:
build_and_test:
stage: build
script:
- echo "Building the project..."
- echo "Running tests..."
image: ruby:2.7
Options:
- stage: Specifies the stage to which the job belongs.
- script: Defines the commands to execute for the job.
- image: Specifies the Docker image to use for the job.
Source: https://docs.gitlab.com/ee/ci/jobs/README.html
Pipelines
Pipelines are a collection of jobs that are executed in order. Pipelines are triggered by events, such as pushes to the repository or merge requests.
Example pipeline:
stages:
- build
- test
- deploy
build:
stage: build
script:
- echo "Building the project..."
image: ruby:2.7
test:
stage: test
script:
- echo "Running tests..."
image: ruby:2.7
deploy:
stage: deploy
script:
- echo "Deploying to production..."
image: ruby:2.7
only:
- master
Options:
- stages: Defines the stages of the pipeline. Each stage must be executed in order.
- jobs: Defines the jobs that will be executed within the pipeline.
- trigger: Specifies the event that will trigger the pipeline.