Developer and Text Tools

Writing .gitlab-ci.yml Without Guesswork: Rules, Needs and Validation

How GitLab reads stages, rules, needs, cache and artifacts, the mistakes that break pipelines, and how to check a .gitlab-ci.yml before you push it.

Saizul Amin
Saizul Amin7 Nov 2026 · 5 min read
Writing .gitlab-ci.yml Without Guesswork: Rules, Needs and Validation

A .gitlab-ci.yml file looks simple until a pipeline refuses to start with a one-line error, or worse, starts but skips the deploy job without saying why. Most of these problems come from a handful of keywords whose behaviour is not obvious from the YAML alone. This guide walks through them and shows how the GitLab CI YAML Builder and Validator helps you write the file from forms and check it with line numbers before you commit.

Stages and jobs

Every top-level key that is not a keyword is a job. GitLab groups jobs into stages, and by default a stage only starts when every job in the previous stage has passed. If you leave out the stages keyword, GitLab assumes build, test and deploy. A job without a stage is placed in test, which is why a job sometimes appears in a column you did not expect.

Hidden jobs, whose names start with a dot, never run. They exist to be reused with extends. When a job extends a template, GitLab merges the two: mappings such as variables are combined key by key, while lists such as script are replaced entirely. That second rule surprises people who expect their script lines to be appended.

Needs: running jobs early

The needs keyword turns the pipeline into a graph. A job with needs starts as soon as the listed jobs finish, instead of waiting for its whole stage. In a typical Node.js pipeline, lint and unit tests only need the install job, and the build only needs those two. On a busy runner fleet this can save many minutes.

Needs have strict rules. The needed job must exist, must not be hidden, and must not sit in a later stage. Two jobs that need each other form a cycle, and GitLab rejects the whole file. An empty list, needs: [], is legal and means the job starts immediately when the pipeline is created. The builder's graph tab draws each needs entry as an arrow, so a missing or backwards link is easy to spot.

Rules: the first match wins

Rules decide whether a job is added to a pipeline. GitLab checks them from top to bottom and uses the first rule whose conditions are true. If no rule matches, the job is simply not added, with no error. Each rule can set when, so one rule can make a deploy manual on the default branch while another adds nothing elsewhere.

The if expressions have their own small language. Variables start with a dollar sign, strings are quoted, == and != compare, =~ and !~ match a regular expression written between slashes, and && and || combine conditions. A common typo is a single equals sign, which is a syntax error. The validator parses every expression, so the mistake is reported on the right line.

Workflow rules work the same way but decide whether a pipeline is created at all. They are the usual fix for duplicate pipelines, where both a branch pipeline and a merge request pipeline start for the same push.

The merge request surprise

Jobs without rules do not run in merge request pipelines. If your workflow allows merge request pipelines but your jobs have no rules, GitLab creates a pipeline with nothing in it and reports an error. The simulator in the tool makes this visible: choose the merge request preset and every job without a matching rule is shown as not running, with the reason next to it.

Cache and artifacts are different things

Cache speeds up later runs of the same job by keeping downloaded dependencies, and it is not guaranteed to be there. Artifacts pass files from one job to later jobs and can be downloaded from the pipeline page. Use a cache key based on your lock file, such as package-lock.json, so the cache changes when dependencies change. Give artifacts an expire_in such as 1 week; the value must be a duration GitLab understands, and a typo like 2 weks fails validation.

Checking a file you already have

Switch the tool to validate mode and paste or open your .gitlab-ci.yml. It is parsed with js-yaml, including anchors, merge keys and !reference tags. The checker then reports unknown keywords with a suggested correction, jobs without a script, stages that are not listed, needs that point nowhere, only and except mixed with rules, bad durations, images without a pinned tag and secrets typed directly into scripts. Each problem carries a line number, and tapping it shows the line.

If the file looks right, press Import into the form to keep working on it with the editor. Keys that the form does not cover are carried through untouched.

A short checklist before you push

  • Pin every image to a version tag rather than latest.
  • Use rules instead of only and except.
  • Prefer needs for jobs that only depend on one or two earlier jobs.
  • Add allow_failure: true to manual jobs that should not block the pipeline.
  • Give every artifact an expiry and every cache a key based on a lock file.
  • Simulate a push, a merge request and a tag, and check that the right jobs run.

The tool is a local checker, not GitLab's own lint, and it cannot see files pulled in with include or variables set in your project settings. Use it to catch the structural mistakes early, then let the pipeline editor in GitLab confirm the final file.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages