Using GitLab CI/CD well
A well-built GitLab pipeline tests, builds and deploys on every push, hands-free. Here's how I structure a clean .gitlab-ci.yml: stages, cache, artifacts, environments and conditional deployment.
A well-built GitLab pipeline tests, builds and deploys on every push, hands-free. Here's how I structure a clean .gitlab-ci.yml: stages, cache, artifacts, environments and conditional deployment.
GitLab CI reads a .gitlab-ci.yml file at the root. It defines jobs, grouped into stages that run in sequence: a stage only starts if the previous one succeeded. Jobs run on runners — machines that execute your commands in a container.
Three stages are enough for most apps: test, build, deploy. Cache avoids reinstalling dependencies in each job; artifacts carry the build.
Cache = what you rebuild (node_modules). Artifact = what you pass on (the build). Don’t confuse them.
only: [main] or rules: prod deploys only from the right branch, never from a feature branch.
Declare staging and production: GitLab tracks what’s deployed where, and enables one-click rollback.
Never a secret in the YAML: put them in masked CI/CD variables, in the project settings.
The same deploy script should run in CI and locally — that’s the principle of make a deployment boring.
A good GitLab pipeline is invisible: it tests and deploys without you thinking about it, and stops you cold when something breaks. Three stages, cache, artifacts, well-stored secrets — and you push with peace of mind.
GitLab CI/CD docs ↗