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
- Download the GitLab Runner binary for Windows from the official GitLab downloads page.
- Move the binary to
C:\GitLab-Runner
. - Open PowerShell as Administrator and install GitLab Runner:
cd C:\GitLab-Runner .\gitlab-runner.exe install .\gitlab-runner.exe start
- Verify installation:
.\gitlab-runner.exe --version
Installing GitLab Runner on Linux
- 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
- Assign execution permissions:
chmod +x /usr/local/bin/gitlab-runner
- Install and start GitLab Runner:
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner sudo gitlab-runner start
- Verify installation:
gitlab-runner --version
Step 2: Configure Local GitLab CI/CD Pipelines
Register GitLab Runner
To connect GitLab Runner with your GitLab instance:
- Obtain the registration token from GitLab by navigating to:
Settings -> CI/CD -> Runners
. - Register the runner using the following command:
gitlab-runner register
- 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:
- Install Docker (Download Here).
- Register GitLab Runner with Docker:
gitlab-runner register --executor docker --docker-image ubuntu:latest
- 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.