Format GitLab CI/CD Pipeline YAML (.gitlab-ci.yml)
Format and validate GitLab CI/CD pipeline YAML files with correct job structure, stage definitions, rules, artifacts, and caching configuration. Catch common .gitlab-ci.yml errors.
Detailed Explanation
GitLab CI/CD YAML Formatting
GitLab CI/CD pipelines are defined in .gitlab-ci.yml files at the repository root. This YAML file describes stages, jobs, scripts, rules, artifacts, and caching — the entire CI/CD workflow.
Pipeline Structure
stages:
- build
- test
- deploy
variables:
NODE_VERSION: "20"
DOCKER_REGISTRY: registry.gitlab.com
default:
image: node:${NODE_VERSION}-alpine
cache:
key:
files:
- package-lock.json
paths:
- node_modules/
build:
stage: build
script:
- npm ci
- npm run build
artifacts:
paths:
- dist/
expire_in: 1 hour
test:unit:
stage: test
script:
- npm ci
- npm run test:unit
coverage: '/Lines\s*:\s*(\d+\.?\d*)%/'
test:e2e:
stage: test
image: cypress/included:latest
script:
- npm ci
- npm run test:e2e
artifacts:
when: on_failure
paths:
- cypress/screenshots/
deploy:production:
stage: deploy
script:
- ./deploy.sh
environment:
name: production
url: https://app.example.com
rules:
- if: $CI_COMMIT_BRANCH == "main"
when: manual
- when: never
GitLab-Specific YAML Features
Hidden jobs — Jobs starting with
.are templates, not executed:.test-template: script: - npm testExtends keyword — Inherit from template jobs:
test:unit: extends: .test-template variables: TEST_SUITE: unitYAML anchors — GitLab supports standard YAML anchors for reuse, and they are commonly used alongside
extendsInclude — Split pipeline config across multiple files:
include: - local: .gitlab/ci/build.yml - local: .gitlab/ci/test.yml - template: Security/SAST.gitlab-ci.yml
Common Formatting Issues
- Indentation under
script:— Script lines are a YAML sequence, each prefixed with- - Multi-line scripts — Use
|for complex scripts instead of multiple-entries - Rules vs. only/except — Modern GitLab CI uses
rules:instead of the deprecatedonly:/except: - Variable expansion —
${VARIABLE}syntax in YAML values must be understood by the formatter
Validation
GitLab provides a CI Lint tool (/ci/lint in any project) that validates the YAML structure. Running your formatted file through CI Lint catches structural errors before pushing.
Use Case
GitLab CI/CD YAML formatting is essential for teams managing complex pipelines. A well-formatted .gitlab-ci.yml makes it easy to understand the pipeline flow, review changes, and debug failures. Teams with large monorepo pipelines (50+ jobs) especially benefit from consistent formatting, template organization, and proper use of includes to split the pipeline across multiple files.