GitLab YAML: Every Full Stack developers Helper to deliver a smooth Project

ap45

Arpit Parekh

Posted on October 15, 2023

GitLab YAML: Every Full Stack developers Helper to deliver a smooth Project

What is GitLab YAML?

Imagine you have a big box where you keep all your toys and games. This box is like a special helper for you to keep everything organized and neat. Just imagine how your mom would be happy to see this stuff organized right?

Now, Git is a tool that helps grown-ups (like computer programmers) keep their computer code organized. Instead of toys, they have lots and lots of computer code, which is like a set of instructions telling the computer what to do.

In Git, there's a special file called "git.yml," but it's actually ".gitlab-ci.yml" (the ".yml" part is like the box's special label). This file is like a set of rules written down on how to do things with the computer code. It's like a recipe book for cooking but for the computer.
In this "recipe book," you write down steps like how to build a cool tower using building blocks. In computer language, it might say things like "first, get all the building blocks, then stack them up to make a tower ."
This special ".gitlab-ci.yml" recipe book helps the computer follow the steps automatically. So, when a programmer adds some new instructions (like adding a block to the tower), Git and this ".gitlab-ci.yml" recipe book make sure everything is done correctly.

Just like you can have different recipes for making different meals, programmers use different ".gitlab-ci.yml" recipes for different parts of their big projects. It's like having a recipe for making a cake and another one for making cookies.
So, Git and ".gitlab-ci.yml" help people work together smoothly, like a team of toy builders following a plan to create amazing things!

Enough with Analogies! The next part is for nerdy geeks!

GitLab CI/CD is a tool that automates the process of integrating code changes, testing them, and then delivering the updates to production.
At the heart of GitLab CI/CD is the gitlab.yml file, a configuration file written in YAML (YAML Ain't Markup Language). This file is where the magic happens, specifying how your project should be built, tested, and deployed.
In this file, you specify the list of things you want to do, like test and deploy your application. This file follows the YAML format and has its own special syntax.
You can name this file anything you want, but .gitlab-ci.yml is the most common name. Use the pipeline editor to edit the .gitlab-ci.yml file and test the syntax before you commit changes.

Runners

Runners are the agents that run your jobs. These agents can run on physical machines or virtual instances. In your .gitlab-ci.yml file, you can specify a container image you want to use when running the job. The runner loads the image and runs the job either locally or in the container.

Pipelines

Pipelines are made up of jobs and stages:
• Jobs define what you want to do. For example, test code changes, or deploy to a staging environment.
• Jobs are grouped into stages. Each stage contains at least one job. Typical stages might be build, test, and deploy.

Variables

GitLab CI allows you to set your own variables in .gitlab-ci.yml. These variables are available in the job environment when it executes. These variables are stored in the Git repository and are meant to store non-sensitive project configuration. These variables can be used later in all executed commands and scripts. Credentials and other secrets should be stored as Secret Variables instead.

GitLab YAML as Your Recipe

Imagine you're making a classic Indian dish like "Paneer Tikka." The recipe lists ingredients, their quantities, and the steps to prepare the dish. GitLab YAML does something similar for your project.

Just like a recipe guides you in creating a perfect Paneer Tikka, GitLab YAML guides your project through each step, ensuring a delicious outcome—successful project completion!

CI/CD Pipelines

Let's delve into a real-life example: setting up a Continuous Integration/Continuous Deployment (CI/CD) pipeline for a web application using GitLab YAML.

Example GitLab YAML for a CI/CD Pipeline

stages:
  - build
  - test
  - deploy

build:
  stage: build
  script:
    - npm install
    - npm run build

test:
  stage: test
  script:
    - npm run test

deploy:
  stage: deploy
  script:
    - deploy_to_production.sh

Enter fullscreen mode Exit fullscreen mode

Here's how this GitLab YAML translates into our cooking analogy:

1. Build Stage: Preparing Ingredients

The 'build' stage in GitLab YAML can be similar to gathering all the essential ingredients and prepping them for your recipe.
You start by assembling all the spices, vegetables, and other necessary elements. This is exactly what we do before starting a project, installing the dependencies and tools your project needs—the 'npm install' step. Just like making sure you have all the spices in place for a spicy curry, 'npm install' ensures you have all the necessary packages for your application.

Next, we move on to the actual cooking. When you turn on the stove and start mixing and sautéing the ingredients, it's like running 'npm run build.' This step compiles your code, aligning with mixing ingredients and preparing the base of your curry. The 'build' process creates a solid base, just like a well-prepared curry base sets the tone for a delicious meal.

2. Test Stage: Quality Assurance

The 'test' stage in GitLab YAML mirrors the critical step of tasting your dish as it's being prepared.
So when your curry or gravy is done for a Paneer tikka what you do? You ensure it has the right sweetness and texture, and if you like spicy things you ensure it has the right amount of spiced flavor to it.
Similarly, 'npm run test' checks your code to ensure it's as per the desired quality. Running tests is a very important step as it ensures your application works as expected. What I have learned from my experience so far, never ever miss testing of your application, no matter how much time it takes.
Testing is like double-checking if the flavors of your dish have blended well, making sure everything is in harmony before moving forward.

3. Deploy Stage: Serving the Dish

The 'deploy' stage in GitLab YAML is like the final plating and presentation of your culinary creation.

Picture you've just finished cooking an exquisite Paneer tikka. Plating the various components, arranging them aesthetically, and adding garnishes is akin to 'deploying' in the coding world. 'deploy_to_production.sh' script represents this final presentation—the dish is ready to be served, just as your application is ready to be used.
Deploying is the moment when you step back and admire your beautifully plated meal, ready to be enjoyed, just as your application is now ready to be experienced by users.

Advantages:

Standardization: GitLab YAML provides a standardized and clear way to define project configurations, ensuring consistency across the team.

Automation: It automates various processes like building, testing, and deploying, saving time and reducing manual errors.

Easy Version Control: As YAML files are text-based, they can be easily version-controlled using Git, enabling collaborative development.

Disadvantages:
Learning Curve: Understanding YAML syntax and mastering its intricacies can be challenging for newcomers, potentially slowing down initial development.

Complex Configurations: For complex projects, the YAML file can become lengthy and complex, making it hard to manage and troubleshoot.

Mistakes to Avoid When Using GitLab YAML

Improper Indentation: YAML relies heavily on indentation. A small indentation mistake can break the entire configuration.

Overly Complex Pipelines: Keep your CI/CD pipeline simple and focused. Overly complex pipelines can be difficult to manage and understand.

Not Testing Changes: Before pushing changes to your repository, test the YAML configuration locally to catch any syntax errors or issues.

Conclusion: Sailing Smooth with GitLab YAML

Just like a chef carefully plans their recipes for a smooth cooking journey, gitlab.yml helps us plan and automate the steps for our code's journey from the cutting board to the dining table.

Think of gitlab.yml as your recipe card. It lists down the ingredients (your code), the steps to cook (test, build, deploy), and how to present the final dish (deployment). And hey, just like adding a pinch of spice, we can throw in some fun elements, making our coding experience smooth and happy!

In this blog, we've explored GitLab YAML with few examples, showcasing how it's your go-to tool for effective project management. This blog explains the basics of gitlab.yml , but It has a lot more depth to it. As I move forward I will explore more and will publish more content on this, till then thanks for Reading! Cheers😊

References:
https://about.gitlab.com/blog/2015/12/14/getting-started-with-gitlab-and-gitlab-ci/
https://docs.gitlab.com/ee/ci/
https://dx.appirio.com/ci-cd-gitlab/gitlab-yml/
https://codefresh.io/learn/gitlab-ci/what-is-the-gitlab-ci-yml-file-and-how-to-work-with-it/

💖 💪 🙅 🚩
ap45
Arpit Parekh

Posted on October 15, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related