4 Hours

3. GitLab CI: Complete Pipeline Guide

GitLab CI

GitLab CI: Complete Pipeline Mastery

GitLab CI is a powerful CI/CD platform built into GitLab. It uses a YAML configuration file (.gitlab-ci.yml) to define pipelines. GitLab CI is known for its simplicity, scalability, and tight integration with the GitLab ecosystem. Understanding GitLab CI is essential for teams using GitLab for version control.

The key insight is that GitLab CI pipelines are defined using a declarative YAML syntax. Pipelines consist of stages, jobs, and scripts. Stages define the order of execution (e.g., build, test, deploy). Jobs run in parallel within the same stage. Scripts are the commands that execute. GitLab CI supports many advanced features including: matrix testing, caching, artifacts, environments, and approval workflows.

This module covers GitLab CI architecture, pipeline syntax, job configuration, runners, and advanced features.

GitLab CI Pipeline Structure

A GitLab CI pipeline is defined in a file called .gitlab-ci.yml at the root of your repository. The basic structure includes: stages (ordered list of stage names), jobs (named objects under the stages), each job has script (commands to run), stage (which stage it belongs to), and optional attributes like artifacts, cache, and only/except rules.

Pipelines are triggered automatically on git push, merge requests, or schedule. You can also trigger them manually via the GitLab UI or API. Understanding the pipeline structure is essential for building effective CI/CD workflows in GitLab.

Text
stages:
  - build
  - test
  - deploy

variables:
  NODE_VERSION: "18"

cache:
  paths:
    - node_modules/

before_script:
  - apt-get update -qy
  - apt-get install -y nodejs
  - npm install -g npm@latest

build_job:
  stage: build
  script:
    - echo "Building the application..."
    - npm install
    - npm run build
  artifacts:
    paths:
      - dist/

test_job:
  stage: test
  script:
    - echo "Running tests..."
    - npm test
  dependencies:
    - build_job

deploy_job:
  stage: deploy
  script:
    - echo "Deploying to production..."
  environment:
    name: production
  only:
    - main

GitLab Runners

GitLab runners are the agents that execute pipeline jobs. They can be hosted by GitLab (shared runners) or self-hosted. Runners can be configured to run in specific environments (e.g., Docker, Kubernetes, shell). They can be tagged to run specific jobs. Understanding runners is essential for optimizing pipeline performance and reliability.

Shared runners are convenient but may have restrictions on resources. Self-hosted runners give you full control but require maintenance. You can scale runners dynamically using Kubernetes or auto-scaling groups. Choosing the right runner strategy is important for large-scale CI/CD implementations.

Step-by-Step: GitLab CI Pipeline

Follow these steps to build a production-grade GitLab CI pipeline.

Step 1: Create .gitlab-ci.yml
Create the file at the root of your repository:
stages:<br> - build<br> - test<br> - package<br> - deploy

Step 2: Add build job
build:<br> stage: build<br> image: node:18<br> script:<br> - npm install<br> - npm run build<br> artifacts:<br> paths:<br> - dist/<br> expire_in: 1 week

Step 3: Add test job
test:<br> stage: test<br> image: node:18<br> script:<br> - npm install<br> - npm test<br> coverage: <strong><code>/All files\s*\|\s*(\d+.\d+)/

Step 4: Add package job
package:<br> stage: package<br> image: docker:latest<br> services:<br> - docker:dind<br> script:<br> - docker build -t myapp:$CI_COMMIT_SHORT_SHA .<br> - docker push myapp:$CI_COMMIT_SHORT_SHA

Step 5: Add deploy job
deploy:<br> stage: deploy<br> image: alpine:latest<br> script:<br> - echo <strong><code>Deploying to production...
- echo App version: $CI_COMMIT_SHORT_SHA
environment:
name: production
url: https://myapp.com
only:
- main
when: manual

Step 6: Add rules for Merge Requests
rules:<br> - if: $CI_MERGE_REQUEST_ID<br> when: manual<br> - if: $CI_COMMIT_BRANCH == <strong><code>main
when: on_success

Try It Yourself: Add a linting job to the pipeline. Add a security scanning job using GitLab's built-in SAST. Add a Slack notification when the pipeline completes.

Knowledge Check

Ready to test your understanding of 3. GitLab CI: Complete Pipeline Guide?

Back