GitLab CI YAML Syntax
# CHAPTER 3
GitLab CI YAML Syntax
1. Introduction
You understand the theory of Stages and Jobs, but GitLab robots do not speak English; they speak YAML. To instruct GitLab CI/CD on how to build, test, and deploy your application, you must create a specific configuration file in the root directory of your repository. This file is the brain of your DevOps operation. In this chapter, we will learn how to write the.gitlab-ci.yml file. We will master YAML syntax, define our Stages, configure Jobs with terminal scripts, and introduce dynamic Variables to make our pipelines flexible.
2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the strict formatting rules of YAML.
-
Create a
.gitlab-ci.ymlfile.
-
Define custom
stagesglobally.
-
Write a
joband assign it to a specific stage.
-
Write terminal commands using the
scriptkeyword.
3. Beginner Explanation
Imagine you are writing a recipe for a robotic chef.- You cannot just write: "Make a cake." The robot won't understand.
- You must write a highly structured, bulleted list.
- Stage: Baking
- Job: Mix Ingredients
- Step 1: Put 2 cups of flour in the bowl.
- Step 2: Add 1 cup of sugar.
- Step 3: Turn on mixer.
YAML (YAML Ain't Markup Language) is simply a way to write these bulleted lists so that a computer can read them perfectly without getting confused.
4. The .gitlab-ci.yml File
To activate GitLab CI/CD, you MUST create a file named exactly .gitlab-ci.yml in the root of your repository. The moment you push this file to GitLab, the pipeline engine turns on automatically.
The Golden Rules of YAML:
-
1.
Spaces, Not Tabs: YAML uses indentation to understand hierarchy. You MUST use spaces (usually 2 spaces per level). If you use the
Tabkey, the pipeline will crash instantly.
-
2.
Key-Value Pairs: It works like a dictionary:
key: value.
5. Writing Your First Pipeline
Let's dissect a complete, basic.gitlab-ci.yml file.
6. Mini Project: Create Basic CI Workflow
Let's write this code and watch the robots execute it.Step-by-Step Walkthrough:
-
1.
Open your terminal and clone your
gitlab-ci-practicerepository to your local machine.
-
2.
Navigate into the folder:
cd gitlab-ci-practice
-
3.
Create the configuration file:
touch .gitlab-ci.yml
- 4. Open the file in your code editor (like VS Code).
- 5. Paste the exact YAML code from Section 5 above into the file. Save it.
- 6. Commit and push the code:
yaml variables: # This variable is available to ALL jobs in the pipeline WEBSITENAME: "My Awesome App"
deployjob: stage: deploy script:
- echo "Deploying $WEBSITE_NAME to the server!"
yaml script:
- npm install
- npm run build
- npm test
12. Summary
In Chapter 3, we gave our robotic factory its operating instructions. We learned that the .gitlab-ci.yml` file is the absolute core of the GitLab DevOps engine. By mastering YAML syntax, we successfully defined custom chronological Stages, authored distinct Jobs, and provided exact terminal execution scripts. We executed our first real pipeline, witnessing the seamless translation of YAML code into a visual, automated workflow in the GitLab dashboard.