GitLab CI/CD Pipeline Setup Guide

Introduction

This guide demonstrates how to set up a GitLab CI/CD pipeline that automatically executes a script every time you push changes to a GitLab repository. By the end of this setup, you will have a basic automated pipeline that runs a simple script using GitLab’s .gitlab-ci.yml file.


Prerequisites

  • A GitLab account.

  • Git installed locally.

  • Basic knowledge of shell scripting.

Step 1: Set Up a GitLab Repository

  1. Create a New Project in GitLab:

    • Log into your GitLab account and click New Project on the dashboard.

    • Enter the project name and select your preferred visibility level (e.g., Public, Private).

    • Click Create Project to initialize the repository.

  2. Clone the Repository Locally:

    • Once the repository is created, clone it to your local machine:

        git clone https://gitlab.com/username/your-repository.git
      
    • Replace username and your-repository with your GitLab username and repository name, respectively.

    • Move into the repository’s directory:

        cd your-repository
      

Step 2: Create a Basic Script

  1. Add a Script File (script.sh):

    • In the repository, create a file called script.sh:

        touch script.sh
      
    • Open the file in your preferred editor and add the following code:

        #!/bin/bash
        echo "Hello, GitLab CI!"
      
  2. Grant Execute Permissions:

    • Make the script executable by running:

        chmod +x script.sh
      

Step 3: Define the .gitlab-ci.yml File

The .gitlab-ci.yml file is the configuration file for defining your CI/CD pipeline stages and jobs.

  1. Create the .gitlab-ci.yml File:

    • At the root of your repository, create a new file:

        touch .gitlab-ci.yml
      
    • Open the file and add the following configuration:

        stages:
          - test
      
        run-script-job:
          stage: test
          script:
            - ./script.sh
      

Explanation of Configuration:

  • stages: Defines the stages in the pipeline, with a single stage called test.

  • run-script-job: The name of the job that will run during the test stage.

  • script: Commands to execute within this job, where we’re running the script script.sh.

Step 4: Push Changes and Trigger the Pipeline

  1. Commit and Push Changes:

    • Add, commit, and push your files to the GitLab repository:

        git add .
        git commit -m "Add basic GitLab CI pipeline"
        git push origin main
      
  2. View the Pipeline Execution:

    • In GitLab, go to CI/CD > Pipelines in your repository.

    • You should see a pipeline running based on the .gitlab-ci.yml configuration. Once the pipeline completes, you’ll see a green checkmark if the job was successful.

    • Click the pipeline to view logs and check if the output shows Hello, GitLab CI! from the script.

Summary

In this guide, you set up a GitLab CI/CD pipeline that automates the execution of a script upon every code push. This setup serves as a foundation for adding more stages, jobs, and tasks to your pipeline, such as testing, building, or deploying applications.

GitLab CI/CD is a powerful tool for automating workflows and improving development efficiency.