![]() |
VOOZH | about |
GitLab CI/CD automates the process of building, testing, and deploying code, ensuring your software is always in a releasable state. At the heart of GitLab's CI/CD pipelines is the .gitlab-ci.yml file, which defines the stages, jobs, and commands that need to be executed in a CI/CD pipeline.
In this article, we’ll walk through how to create a .gitlab-ci.yml file in GitLab, discuss its structure, and provide some best practices for using it effectively.
The .gitlab-ci.yml file is a YAML-based configuration file placed in the root of your GitLab repository. It tells GitLab how to run CI/CD jobs, which include steps such as compiling code, running tests, deploying applications, and more. Each time a pipeline is triggered (e.g., by a code push or merge request), GitLab Runner reads the .gitlab-ci.yml file and executes the jobs according to the defined stages and conditions.
Before writing your .gitlab-ci.yml file, you should understand how stages work in GitLab pipelines. A pipeline consists of multiple stages, which are executed in a defined order. Each stage contains one or more jobs that will run concurrently. The most common stages include:
These stages form the backbone of a pipeline. You can add custom stages depending on your project’s requirements.
To create the .gitlab-ci.yml file, follow these steps:
stages:
- build
- test
- deploy
Now that you've defined your stages, you can create jobs for each stage. A job is a script that GitLab Runner will execute. Each job should belong to a specific stage. For example, you can create a build job, a test job, and a deploy job.
stages:
- build
- test
- deploy
# Job for the build stage
build_job:
stage: build
script:
- echo "Building the project..."
- make build
# Job for the test stage
test_job:
stage: test
script:
- echo "Running tests..."
- make test
# Job for the deploy stage
deploy_job:
stage: deploy
script:
- echo "Deploying to production..."
- ./deploy.sh
environment:
name: production
In this example:
You can specify a Docker image for each job to define the environment in which it runs. This is useful when you need specific versions of tools or languages.
stages:
- build
- test
- deploy
# Build stage
build_job:
stage: build
image: node:14
script:
- npm install
- npm run build
# Test stage
test_job:
stage: test
image: python:3.9
script:
- pip install -r requirements.txt
- pytest
# Deploy stage
deploy_job:
stage: deploy
image: alpine:latest
script:
- ./deploy.sh
In this example:
You can set conditions for when jobs should run, using keywords like only, except, and rules.
stages:
- build
- test
- deploy
build_job:
stage: build
script:
- make build
only:
- main
test_job:
stage: test
script:
- make test
only:
- merge_requests
deploy_job:
stage: deploy
script:
- ./deploy.sh
only:
- tags
Artifacts allow you to pass files between jobs and stages. For example, the build stage might produce binaries that are needed in the test or deploy stage.
stages:
- build
- test
- deploy
build_job:
stage: build
script:
- make build
artifacts:
paths:
- build/
test_job:
stage: test
script:
- make test
dependencies:
- build_job
In this example:
You can define environment variables in your .gitlab-ci.yml file or through the GitLab UI. Environment variables can be useful for setting credentials, configuration options, or secrets.
stages:
- build
- deploy
build_job:
stage: build
script:
- npm run build
variables:
NODE_ENV: production
deploy_job:
stage: deploy
script:
- ./deploy.sh
environment:
name: production
variables:
DEPLOY_KEY: $DEPLOY_KEY
In this example:
Once you’ve created and committed the .gitlab-ci.yml file, GitLab will automatically trigger the pipeline whenever code is pushed to the repository.
git add .gitlab-ci.yml
git commit -m "Add CI/CD pipeline"
git push origin main