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.
More on GitLab CI
How does GitLab CI/CD work at a high level?BeginnerWhat is a GitLab Runner and what executors exist?IntermediateWhat are the types of runners (shared, group, project) and tags?IntermediateHow do `rules:` work and how do they replace `only/except`?IntermediateWhat are workflow rules and how do they prevent duplicate pipelines?AdvancedHow do artifacts and cache differ in GitLab CI?Intermediate