GitLab CI/CD Pipelines Explained
GitLab CI/CD (Continuous Integration/Continuous Deployment) is a powerful system for automating the process of building, testing, and deploying applications. The pipeline is defined in a .gitlab-ci.yml
file, which resides in the root of the repository.
How GitLab CI/CD Works
- Stages: Pipelines have different stages, such as
build
,test
, anddeploy
. - Jobs: Each stage contains jobs that run specific tasks.
- Runners: Jobs are executed by GitLab Runners, which are workers that process the jobs.
- Triggers: Pipelines are triggered by events such as pushing code, merging branches, or manually starting a job.
Example of a GitLab CI/CD Pipeline (.gitlab-ci.yml
)
Below is a simple GitLab CI/CD YAML file:
stages:
- build
- test
- deploy
# Build Stage
build-job:
stage: build
image: node:16
script:
- echo "Installing dependencies..."
- npm install
- echo "Building the project..."
- npm run build
artifacts:
paths:
- dist/
# Test Stage
test-job:
stage: test
image: node:16
script:
- echo "Running tests..."
- npm test
dependencies:
- build-job
# Deploy Stage
deploy-job:
stage: deploy
image: alpine:latest
script:
- echo "Deploying application..."
- echo "Application deployed successfully!"
only:
- main # Runs only on the main branch
Explanation of the .gitlab-ci.yml
File
1. Defining Stages
stages:
- build
- test
- deploy
- Pipelines run through these stages in sequential order.
- Each stage can have multiple jobs that run in parallel.
2. Build Stage (build-job
)
build-job:
stage: build
image: node:16
script:
- echo "Installing dependencies..."
- npm install
- echo "Building the project..."
- npm run build
artifacts:
paths:
- dist/
- Uses Node.js 16 as the environment.
- Installs dependencies and builds the project.
- Stores build artifacts (
dist/
directory) so the next stages can use them.
3. Test Stage (test-job
)
test-job:
stage: test
image: node:16
script:
- echo "Running tests..."
- npm test
dependencies:
- build-job
- Uses Node.js 16 for running tests.
- Ensures that tests are only executed after the
build-job
completes. - Inherits build artifacts from the
build-job
.
4. Deploy Stage (deploy-job
)
deploy-job:
stage: deploy
image: alpine:latest
script:
- echo "Deploying application..."
- echo "Application deployed successfully!"
only:
- main
- Uses Alpine Linux as the environment.
- Runs a deployment script (this would usually include SSH, FTP, or cloud deployment commands).
- Runs only when changes are pushed to the
main
branch.
Additional Features
1. Using Environment Variables
You can define secrets such as API keys in GitLab Settings → CI/CD → Variables and reference them in .gitlab-ci.yml
:
script:
- echo "Deploying with API Key: $API_KEY"
2. Caching Dependencies
To speed up the pipeline by reusing dependencies:
cache:
paths:
- node_modules/
3. Running Jobs on Specific Branches
To limit jobs to specific branches:
only:
- main
- develop
4. Running Jobs Manually
To trigger a job manually from the GitLab UI:
manual-job:
stage: deploy
script:
- echo "This job must be triggered manually."
when: manual
Conclusion
GitLab CI/CD pipelines allow for automated testing, building, and deployment of applications. Using .gitlab-ci.yml
, you can define workflows, use caching, control environments, and secure your deployment process.