Great! Letโs go through a basic example of GitLab CI/CD pipeline YAML that demonstrates how the stages keyword is used.
๐ง What is stages in GitLab CI/CD?
The stages keyword defines the sequence of execution in your pipeline. Jobs are grouped under these stages, and GitLab runs the stages in order: one must complete successfully before the next begins.
โ Basic GitLab CI YAML Example
stages:
- build
- test
- deploy
# Job in "build" stage
build-job:
stage: build
script:
- echo "Compiling the app..."
- echo "Build complete!"
# Job in "test" stage
test-job:
stage: test
script:
- echo "Running unit tests..."
- echo "All tests passed!"
# Job in "deploy" stage
deploy-job:
stage: deploy
script:
- echo "Deploying the app..."
- echo "Deployment successful!"
Code language: PHP (php)
๐ Explanation
๐น stages Section
stages:
- build
- test
- deploy
- This declares three sequential stages:
build,test, anddeploy. - GitLab will execute jobs in this order, waiting for all jobs in one stage to pass before moving to the next.
๐น Jobs and Their Assigned Stages
build-job:
build-job:
stage: build
script:
- echo "Compiling the app..."
Code language: PHP (php)
- This job is part of the
buildstage and will run first.
test-job:
test-job:
stage: test
script:
- echo "Running unit tests..."
Code language: PHP (php)
- This runs only after all build jobs succeed.
deploy-job:
deploy-job:
stage: deploy
script:
- echo "Deploying the app..."
Code language: PHP (php)
- This runs last, only if all test jobs succeed.
๐ง Key Notes
- If a stage has multiple jobs, GitLab runs them in parallel (if runners are available).
- If any job in a stage fails, subsequent stages are skipped.
- You must explicitly assign
stage:to every job (or it defaults totestif not defined).