CI/CD Pipeline in GitLab Runner on Linux

Introduction to CI/CD with GitLab Runner on Linux

Continuous Integration and Continuous Deployment (CI/CD) form the backbone of modern software delivery. Leveraging GitLab and its powerful Runner component on a Linux host provides a robust, scalable, and fully automated pipeline for building, testing, and deploying code. This article dives deep into setting up and optimizing a CI/CD pipeline using GitLab Runner on Linux, covering architecture, configuration, best practices, and advanced topics.

Prerequisites Environment

  • GitLab Instance: Either self-hosted or GitLab SaaS.
  • Linux Server: A machine running a modern Linux distro (Ubuntu, CentOS, Debian, etc.).
  • Root or Sudo Access: Required for installing and configuring the Runner service.
  • Docker or Shell Executor: Based on your isolation and dependency requirements.
  • Network Connectivity: Ensure outgoing connections to gitlab.com (or your GitLab server).

Installing GitLab Runner on Linux

Installation differs slightly by distribution. Below is an example for Ubuntu/Debian:

sudo apt-get update
sudo apt-get install -y curl
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh  sudo bash
sudo apt-get install -y gitlab-runner
  

For CentOS/RHEL:

sudo yum install -y curl
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh  sudo bash
sudo yum install -y gitlab-runner
  

Registering a Runner

Once installed, register the Runner with your GitLab instance:

sudo gitlab-runner register 
  --url https://gitlab.com/ 
  --registration-token YOUR_TOKEN 
  --executor docker 
  --description linux-docker-runner 
  --docker-image alpine:latest
  

Key options explained:

  • –url: GitLab server URL.
  • –registration-token: Found in your project’s Settings gt CI/CD gt Runners.
  • –executor: shell, docker, docker machine, kubernetes, etc.
  • –docker-image: Default image for jobs if using Docker executor.

CI/CD Pipeline Architecture

A typical GitLab CI/CD pipeline consists of multiple stages executed in order. Each stage may contain one or more jobs, which run in isolation on the Runner.

Stage Purpose Typical Jobs
build Compile or assemble artifacts compile, package, lint
test Run unit/integration tests pytest, junit, sonar
deploy Push to staging or production helm, kubectl, scp
cleanup Purge temporary resources docker prune, aws rm

Crafting .gitlab-ci.yml

The pipeline is defined in .gitlab-ci.yml at your repository root. A minimal example:

stages:
  - build
  - test
  - deploy

build_job:
  stage: build
  script:
    - echo Building...
    - make build

test_job:
  stage: test
  script:
    - echo Testing...
    - make test

deploy_job:
  stage: deploy
  script:
    - echo Deploying...
    - make deploy
  only:
    - master
  

Key directives:

  • stages: Order of execution.
  • script: Commands run by the job.
  • only/except: Branch filters.
  • variables: Define environment variables.
  • artifacts: Persist files between stages.
  • cache: Speed up subsequent runs.

Advanced Configuration

Parallel and Matrix Builds

Run multiple jobs concurrently or generate dynamic job matrices:

test_job:
  stage: test
  parallel:
    matrix:
      - VARIANT: [ python3.8, python3.9, python3.10 ]
  script:
    - echo Testing on VARIANT
  

Docker-in-Docker (DinD)

To build Docker images inside your jobs:

build_image:
  image: docker:20.10
  services:
    - name: docker:20.10-dind
      command: [--tls=false]
  variables:
    DOCKER_HOST: tcp://docker:2375/
  script:
    - docker build -t my-app:CI_COMMIT_SHORT_SHA .
  

Kubernetes Executor

For auto-scaling on Kubernetes, register a Runner with the kubernetes executor and configure a config.toml with the cluster API. See the Kubernetes Executor guide.

Best Practices Tips

  • Use Caching Sparingly: Cache only large dependencies (e.g., node_modules, ~/.m2/repository).
  • Secure Variables: Store secrets in CI/CD gt Variables with protection and masking.
  • Limit Parallel Jobs: Avoid running too many concurrent jobs on a single Runner.
  • Monitor Runners: Use GitLab’s monitoring panel or Prometheus exporters.
  • Cleanup Resources: Add a cleanup stage to remove old containers, temp files, cloud resources.
  • Use Artifacts Wisely: Persist only what is necessary (logs, coverage reports, binaries).

Troubleshooting Common Issues

Runner Failing to Connect

  • Ensure the registration token is valid.
  • Verify network connectivity to GitLab server.
  • Check /etc/gitlab-runner/config.toml for proper url and token.

Jobs Stuck in Pending

  • No active Runner available or tag mismatch.
  • Check Runner’s status in project’s Settings gt CI/CD gt Runners.
  • Review logs via sudo journalctl -u gitlab-runner.

Conclusion

Setting up a CI/CD pipeline with GitLab Runner on Linux empowers teams to deliver high-quality software rapidly and reliably. From basic builds to complex Kubernetes deployments, GitLab’s flexibility and extendable architecture handle everything in a unified platform. Follow best practices, secure your pipelines, and continually refine your configuration to keep pace with evolving requirements.

For more details, explore the official GitLab Runner documentation and the broader GitLab CI/CD guides.

Download TXT




Leave a Reply

Your email address will not be published. Required fields are marked *