GitLab CI/CD: Pipelines, Runners & Auto DevOps Guide
In this tutorial, you'll learn about GitLab CI/CD: Pipelines, Runners & Auto DevOps Guide. We cover key concepts, practical examples, and best practices.
GitLab CI/CD pipelines automate builds, tests, and deployments using a .gitlab-ci.yml configuration file with stages that run jobs in sequence.
In this tutorial, you'll learn GitLab CI/CD for automating builds, tests, and deployments. GitLab CI/CD is built directly into GitLab, providing a seamless integration between your code repository and pipelines. By the end, you'll write .gitlab-ci.yml files, configure runners, use environments, and implement deployment strategies.
flowchart TD
A[Git Push] --> B[Pipeline Triggered]
B --> C[Stage: build]
C --> D[Stage: test]
D --> E[Stage: deploy]
E --> F{Environment}
F --> G[Staging]
F --> H[Production]
Writing Your First Pipeline
Create .gitlab-ci.yml in your repository root:
stages:
- build
- test
- deploy
build-job:
stage: build
script:
- echo "Compiling application..."
- mkdir -p build
- echo "Build complete" > build/output.txt
artifacts:
paths:
- build/
test-job:
stage: test
script:
- echo "Running tests..."
- test -f build/output.txt
- echo "Tests passed"
deploy-job:
stage: deploy
script:
- echo "Deploying to production..."
only:
- main
Expected output on commit: A pipeline runs with three sequential stages. Each stage shows its output in the GitLab UI.
Defining Jobs with Docker
Use Docker images for consistent environments:
test:
image: node:18-alpine
stage: test
script:
- npm ci
- npm test
cache:
key: $CI_COMMIT_REF_SLUG
paths:
- node_modules/
GitLab Runners
Runners execute jobs. You can use shared GitLab-hosted runners or install your own:
# Register a self-managed runner
sudo gitlab-runner register \
--url https://gitlab.com/ \
--registration-token YOUR_TOKEN \
--executor docker \
--description "Docker Runner" \
--docker-image alpine:latest
Environments and Deployments
Define deployment environments:
deploy-staging:
stage: deploy
environment:
name: staging
url: https://staging.example.com
script:
- echo "Deploy to staging"
only:
- develop
deploy-production:
stage: deploy
environment:
name: production
url: https://example.com
script:
- echo "Deploy to production"
when: manual
only:
- main
Auto DevOps
Enable Auto DevOps in your project settings for automated CI/CD without writing a pipeline. GitLab automatically detects your language, runs tests, builds a Docker image, and deploys to Kubernetes.
Pipeline Components
| Component | Description | Example |
|---|---|---|
stages |
Execution order | build, test, deploy |
script |
Commands to run | echo "Building" |
image |
Docker image for job | node:18-alpine |
artifacts |
Files to pass between stages | build/ directory |
cache |
Reusable dependencies | node_modules/ |
environment |
Target deployment env | production |
only/except |
Branch filters | only: - main |
when |
Job execution condition | manual, on_success, always |
CI/CD Variables and Secrets
Store sensitive data in GitLab CI/CD variables:
# Access in jobs
deploy:
script:
- echo "$DEPLOY_KEY" > deploy_key.pem
- chmod 600 deploy_key.pem
- ssh -i deploy_key.pem user@server "deploy"
Set variables in Project > Settings > CI/CD > Variables. Masked variables are hidden in job logs.
Child Pipelines and Multi-Project Pipelines
Compose pipelines from multiple projects:
# Trigger a child pipeline from another project
trigger-deploy:
stage: deploy
trigger:
project: my-group/deployment-tool
branch: main
strategy: depend
Child pipelines let you build, test, and deploy across multiple projects in sequence.
GitLab CI/CD Templates
GitLab provides ready-made pipeline templates:
# Include a template
include:
- template: Auto-DevOps.gitlab-ci.yml
- template: Security/SAST.gitlab-ci.yml
- template: Security/Secret-Detection.gitlab-ci.yml
- project: 'my-group/my-shared-config'
file: '/templates/ci.yml'
Common Errors
| Error | Cause | Fix |
|---|---|---|
No CI/CD config found |
Missing .gitlab-ci.yml |
Create file in repository root |
Job stuck |
No available runners | Check runner registration and tags |
Permission denied |
Missing CI/CD token | Check access token scope |
Artifact too large |
Exceeded storage limit | Reduce artifact size or use cache |
Pipeline not triggered |
Branch filter mismatch | Check only / except conditions |
YAML syntax error |
Invalid pipeline config | Use CI Lint tool in GitLab UI |
Image pull failure |
Docker image not found | Verify image name and tag |
Environment URL invalid |
Bad URL format | Use valid HTTPS URL |
Practice Questions
Challenge
Create a .gitlab-ci.yml for a Python application with four stages: build (install dependencies), test (pytest with coverage), lint (flake8), and deploy (to staging and production). Configure caching for pip packages, use Docker images, and set up manual approval for production deployment. Include branch restrictions — staging from develop, production from main.
Real-World Task
Set up a GitLab project with Auto DevOps enabled. Push a sample application, trigger the pipeline, and verify each stage runs successfully. Then install a self-managed GitLab runner on a local machine using Docker executor. Configure it to run your pipelines without using shared runner minutes. This setup is used at DodaTech for internal tools where data security requires self-hosted runners.
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro