GitLab CI/CD Pipeline Patterns#
GitLab CI/CD runs pipelines defined in a .gitlab-ci.yml file at the repository root. Every push, merge request, or tag triggers a pipeline consisting of stages that contain jobs. The pipeline configuration is version-controlled alongside your code, so the build process evolves with the application.
Basic .gitlab-ci.yml Structure#
A minimal pipeline defines stages and jobs. Stages run sequentially; jobs within the same stage run in parallel:
stages:
- build
- test
- deploy
build-app:
stage: build
image: golang:1.22
script:
- go build -o myapp ./cmd/myapp
artifacts:
paths:
- myapp
expire_in: 1 hour
unit-tests:
stage: test
image: golang:1.22
script:
- go test ./... -v -coverprofile=coverage.out
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage.out
deploy-staging:
stage: deploy
image: bitnami/kubectl:latest
script:
- kubectl set image deployment/myapp myapp=$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
environment:
name: staging
url: https://staging.example.com
rules:
- if: $CI_COMMIT_BRANCH == "main"Every job must have a stage and a script. The image field specifies the Docker image the job runs inside. If omitted, it falls back to the pipeline-level default image or the runner’s default.