Write a basic `.gitlab-ci.yml` with build, test and deploy stages where deploy only runs on main.

BeginnerScenarioGitLab CI
Answer

Situation read → Standard staged pipeline.

What I'd do →

stages: [build, test, deploy]

build:
  stage: build
  image: node:22
  script: [npm ci, npm run build]
  artifacts: { paths: [dist/], expire_in: 1 week }

test:
  stage: test
  image: node:22
  script: [npm ci, npm test]

deploy:
  stage: deploy
  script: ./deploy.sh
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

Why → rules replaced only/except, and artifacts from earlier stages are downloaded automatically.

Interview tip → Use rules, not only/except; the latter is legacy.

All CI/CD questions Roadmap: GitLab CI/CD

More on GitLab CI