Page cover image

YAML

YAML is a human-readable data serialization format. It is used for configuration files, deployment manifests, and data exchange between languages with different data structures.

Basic Syntax

Key-Value Pairs:

Simple assignments.

name: John Doe
age: 30

Lists:

Sequential arrays of data.

hobbies:
  - reading
  - hiking
  - coding

Maps:

Nested key-value assignments.

person:
  name: Jane
  age: 25

Comments:

Anything after # is a comment.

# This is a comment
key: value

Advanced Constructs

Multiline Strings:

Using the | character.

description: |
  This is a long string
  spanning multiple lines.

Anchors & Aliases:

Allow for duplicating properties.

base: &base
  name: Everyone
person1:
  <<: *base
  age: 30

Merging Data:

Combine data from two sources.

defaults: &defaults
  adapter: postgres
  host: localhost
development:
  database: mydb
  <<: *defaults

GitLab's .gitlab-ci.yml

Usually, YAML is used to write CI/CD pipelines, we will see a high level overview of how its used in GitLab pipelines.

The .gitlab-ci.yml file, employed by GitLab for CI/CD, is an excellent real-world example of YAML's utility.

Key Components

  1. image: Specifies the Docker image for CI/CD tasks.

  2. stages: Different stages of CI/CD, like build, test, and deploy.

  3. before_script: Commands run before all jobs.

  4. job: Defines commands for a specific stage.

Example Configuration:

image: python:3.9

stages:
  - build
  - test
  - deploy

before_script:
  - pip install -r requirements.txt

build:
  stage: build
  script:
    - echo "Building..."
    # Other build commands.

test:
  stage: test
  script:
    - echo "Testing..."
    - pytest

deploy:
  stage: deploy
  script:
    - echo "Deploying..."
    # Deployment commands.

Advanced Concepts in .gitlab-ci.yml

  1. cache: Caches directories between runs, like dependencies.

  2. variables: Pre-defined environment variables.

  3. tags: Labels assigned to runners to control job execution locations.

Last updated