Post

GitLab CI/CD - Create Your First Pipeline

GitLab CI/CD - Create Your First Pipeline

GitLab CI/CD is a tool used for automating the process of testing, building, and deploying code. It helps developers integrate and deliver software quickly and reliably. Here’s a breakdown of the process of setting up your first pipeline with GitLab CI/CD based on your uploaded notes:

1. Setting Up the Repository

  • Create a New Project: First, go to your GitLab dashboard and create a new project. This can be a blank project, and you should name it something descriptive like GitLab-CI-Demo.

  • Add Code to the Repository: Inside your project, create a new file called hello.py. This will be the code that you want to run using your pipeline. You can write a simple “Hello, World!” Python script in this file.

2. Creating the .gitlab-ci.yml File

  • The .gitlab-ci.yml file is the heart of GitLab CI/CD. This file defines your pipeline: what jobs to run and how to run them.

  • Create the .gitlab-ci.yml File: Add the following content to the .gitlab-ci.yml file:

    1
    2
    3
    4
    5
    6
    7
    8
    
    stages:
      - run
        
    job-to-run-the-code:
      stage: run
      image: python:3.8
      script:
        - python hello.py
    

    Explanation:

    • stages: This defines the sequence of tasks in the pipeline (e.g., building, testing, deploying).

    • job-to-run-the-code: This is a job that belongs to the run stage. It specifies the Python image to use (python:3.8) and defines the script to execute (python hello.py).

  • Commit the .gitlab-ci.yml File: After creating this file, commit it to your repository. It’s best practice to commit to a separate branch (e.g., dev1), and later merge it to the master branch.

3. Viewing and Running the Pipeline

  • GitLab Automatically Runs the Pipeline: Once the .gitlab-ci.yml file is committed, GitLab will automatically detect it and trigger a pipeline.

  • Monitor the Pipeline: You can go to the Build > Pipelines section in GitLab to monitor the progress of the pipeline. It shows the status of each job and provides logs that show details, such as:

    • The GitLab runner’s environment.

    • The output of running your Python file (hello.py), such as “Hello, World!”.

4. Merging to Master and Managing Pipeline Triggers

  • After testing your pipeline, create a merge request from your dev1 branch to master. This helps to manage changes and deploy them to production.

Optional: Using the Pipeline Editor

  • GitLab offers a Pipeline Editor that lets you create pipelines using predefined templates or by starting from a blank pipeline file. This is useful for beginners who may want a guided way of setting up their pipelines.

Summary of Key Concepts:

  • GitLab CI/CD automates building, testing, and deploying your code.

  • The .gitlab-ci.yml file defines the pipeline’s stages and jobs.

  • Pipelines run automatically when you push changes to the repository.

  • You can view pipeline logs to see what’s happening in each job.

  • Merge Requests allow you to manage and trigger pipelines when merging branches.

This post is licensed under CC BY 4.0 by the author.

Trending Tags