GitLab GitLab JobsGitLab YAMLgitlab-ci.ymlPipeline Configuration

Article & Tutorial

GitLab CI/CD YAML Syntax Explained

Learn how GitLab CI/CD YAML defines stages, jobs, scripts, images, variables, rules, needs, artifacts, caches, services, environments, and reusable configuration.

Structured YAML configuration validated into build, test, and deployment jobs
Structured YAML configuration validated into build, test, and deployment jobs
Contents

The .gitlab-ci.yml file describes the jobs that make up a GitLab pipeline. YAML structure matters: indentation uses spaces, lists use ordered entries, and duplicate or misplaced keys can change behavior or make the configuration invalid.

Global and job-level configuration

ScopeCommon purpose
GlobalDefault image, stages, variables, workflow, includes
JobScript, stage, rules, needs, artifacts, cache, environment, services

Essential keywords

  • stages defines the ordered phase names.
  • script lists the commands a job executes.
  • image selects the container image for compatible executors.
  • variables provides non-secret configuration values or references.
  • rules controls when a job is included and how it behaves.
  • needs creates explicit job dependencies and can shorten pipeline duration.
  • artifacts preserves outputs and reports.
  • cache reuses safe dependency data across jobs or pipelines.
  • environment records deployment targets and URLs.

Stages and dependencies

Without explicit needs relationships, jobs typically follow stage ordering. Use needs when a job can start as soon as its required upstream jobs finish. Keep dependency graphs understandable; aggressive parallelism can make failures and resource contention harder to diagnose.

Reusable configuration

Reduce duplication with defaults, YAML anchors where appropriate, extends, and included configuration. Pin external includes to a reviewed reference. Reuse should make policy clearer, not hide important execution or permission behavior.

Validate before committing

  1. Check YAML indentation and data types.
  2. Use GitLab CI Lint or pipeline-editor validation.
  3. Inspect the merged configuration when includes are used.
  4. Confirm rule outcomes for branches, tags, merge requests, and schedules.
  5. Review resolved variables without exposing secret values.
  6. Run the change on a non-production branch and trusted runner.

Common mistakes

MistakeResult
Tabs or inconsistent indentationInvalid or unexpected YAML structure
Secret value in the fileCredential exposure in repository history
Broad catch-all rulesDuplicate or unintended pipelines
Unpinned external includeBehavior changes without repository review
Artifact used as cacheUnnecessary storage and incorrect dependency flow

Treat pipeline YAML as production code: review it, validate it, test it under multiple pipeline sources, and restrict who can change deployment or security-sensitive sections.

GitLab CI/CD YAML syntax reference

Did this help?

Yes No

Join the discussion

0 Comments

Ask questions, add context, or share what worked.

Sign in to join the discussion

No comments yet. Start the discussion.

More from Melochip Editorial

Related articles

Stay in the loop. Get new Melochip articles

Receive Melochip article updates when new articles are available. Explicit opt-in only.

No pre-checked boxes. Unsubscribe support is part of the subscriber foundation.