Skip to main content

Stage

A CI/CD stage is a logical phase of a pipeline, such as build, test, scan, or deploy, that groups jobs and must succeed before the next stage starts.

Understanding CI/CD Stages

What Is a Stage and How Does It Gate the Pipeline

A stage groups related jobs into one phase of delivery. Stages run in sequence, and jobs inside a stage can run in parallel. If any job in a stage fails, later stages do not run by default, which is what stops a failing test from reaching production. A stage only has value if it genuinely blocks.

flowchart TD A[Stage 1: Build] --> B{All jobs pass?} B -->|Yes| C[Stage 2: Test] B -->|No| D[Pipeline blocked] C --> E[Stage 3: Deploy] classDef stage fill:#326CE5,color:#FFFFFF classDef bad fill:#EF4444,color:#FFFFFF classDef good fill:#10B981,color:#FFFFFF class A,B,C stage class D bad class E good

Stages in GitLab CI

YAML
stages:
- build
- test
- scan
- deploy
build-image:
stage: build
script:
- docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
unit-tests:
stage: test
script:
- npm test
lint:
stage: test # runs in parallel with unit-tests
script:
- npm run lint

Stages in GitHub Actions

GitHub Actions has no stages keyword. You get the same effect by ordering jobs with needs.

YAML
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: docker build .
unit-test:
needs: build
runs-on: ubuntu-latest
steps:
- run: npm test
lint:
needs: build
runs-on: ubuntu-latest
steps:
- run: npm run lint
deploy:
needs: [unit-test, lint]
runs-on: ubuntu-latest
steps:
- run: ./deploy.sh

Practical Commands

Bash
gh run rerun 1234567890 --failed
glab ci retry 1234567

In a Jenkinsfile, wrap flaky steps in retry(3) { sh 'npm test' } for transient failures.

Troubleshooting

Symptom Check What to look for
Stage skipped Job conditions if or rules excluding it
Stage hangs Timeout settings Process waiting for input
Parallel jobs conflict Shared state Same files or ports used
Tip

Name stages after purpose, such as Test or Scan, not tools, such as jest or trivy. Tools change, purposes do not.

Remember

Every stage is a gate. If a test stage may fail without blocking, it is decoration.

Security

Production deploy stages should require explicit approval through environment protection rules.

Common Mistake

One huge stage that builds, tests, scans, and deploys. Split concerns so failures are obvious and retries are targeted.

Frequently Asked Questions

What is the difference between a stage and a job?

A stage is a phase like build or test. A job is the unit of work inside it. Jobs in a stage can run in parallel, while stages run in order.

Can stages run in parallel?

Normally no, because each stage waits for the previous one. Features such as GitLab needs or GitHub needs let a job start as soon as its dependencies finish, which shortens pipelines.

How many stages should a pipeline have?

Only as many as there are genuine gates. Too few hides which phase failed, too many sequential stages slows the pipeline for no benefit.

Found a mistake, an outdated step or a broken link?