Set Up a Local CI/CD Pipeline with GitLab Runner | Step-by-Step Guide

Set Up a Local CI/CD Pipeline with GitLab Runner | Step-by-Step Guide

Setting up a Continuous Integration and Continuous Deployment (CI/CD) pipeline locally with GitLab Runner enables developers to automate builds, tests, and deployments efficiently. GitLab CI/CD, combined with GitLab Runner, provides a powerful system for automating software delivery pipelines. This guide walks you through installing GitLab Runner, configuring local GitLab CI/CD pipelines, and automating the build and test processes.

Prerequisites

Before proceeding, ensure you have the following:

  • A GitLab account (self-hosted or GitLab.com)
  • Git installed on your machine (Download Here)
  • GitLab Runner (Download Here)
  • Basic knowledge of Git, CI/CD, and shell scripting

Step 1: Install GitLab Runner on Windows/Linux

GitLab Runner is an application that works with GitLab CI/CD to execute jobs in your pipeline.

Installing GitLab Runner on Windows

  1. Download the GitLab Runner binary for Windows from the official GitLab downloads page.
  2. Move the binary to C:\GitLab-Runner.
  3. Open PowerShell as Administrator and install GitLab Runner:
    cd C:\GitLab-Runner
    .\gitlab-runner.exe install
    .\gitlab-runner.exe start
    
  4. Verify installation:
    .\gitlab-runner.exe --version
    

Installing GitLab Runner on Linux

  1. Open a terminal and download GitLab Runner:
    curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
    
  2. Assign execution permissions:
    chmod +x /usr/local/bin/gitlab-runner
    
  3. Install and start GitLab Runner:
    sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
    sudo gitlab-runner start
    
  4. Verify installation:
    gitlab-runner --version
    

Step 2: Configure Local GitLab CI/CD Pipelines

Register GitLab Runner

To connect GitLab Runner with your GitLab instance:

  1. Obtain the registration token from GitLab by navigating to: Settings -> CI/CD -> Runners.
  2. Register the runner using the following command:
    gitlab-runner register
    
  3. Enter the details:
    • GitLab instance URL
    • Registration token
    • Description of the runner
    • Executor type (e.g., shell, docker)

Create a .gitlab-ci.yml File

A .gitlab-ci.yml file defines your CI/CD pipeline. Create this file in the root of your project with the following content:

stages:
  - build
  - test

build_job:
  stage: build
  script:
    - echo "Building the project..."
    - dotnet build

test_job:
  stage: test
  script:
    - echo "Running tests..."
    - dotnet test

Push this file to your repository, and GitLab will trigger the pipeline.

Step 3: Automate Build and Test Processes

Running the Pipeline Locally

To run the pipeline locally for debugging purposes, use:

gitlab-runner exec shell build_job
gitlab-runner exec shell test_job

Using Docker as an Executor

For better isolation, use Docker instead of the shell executor:

  1. Install Docker (Download Here).
  2. Register GitLab Runner with Docker:
    gitlab-runner register --executor docker --docker-image ubuntu:latest
    
  3. Modify .gitlab-ci.yml to use Docker:
image: mcr.microsoft.com/dotnet/sdk:8.0

stages:
  - build
  - test

build:
  stage: build
  script:
    - dotnet build

test:
  stage: test
  script:
    - dotnet test

Conclusion

Setting up a CI/CD pipeline locally with GitLab Runner allows you to automate software builds and tests efficiently. By installing GitLab Runner, configuring a .gitlab-ci.yml file, and leveraging Docker, you can streamline the development and deployment process. With this setup, you can now expand your pipeline to include deployment steps for a fully automated workflow.

Sandip Mhaske

I’m a software developer exploring the depths of .NET, AWS, Angular, React, and digital entrepreneurship. Here, I decode complex problems, share insightful solutions, and navigate the evolving landscape of tech and finance.

Post a Comment

Previous Post Next Post