GitLab CI/CD: Architecting, Deploying, and Optimizing Pipelines

Architecture Core Concepts

Install Third Party Libraries using before script

In this guide, you’ll learn how to leverage before_script and after_script in GitLab CI/CD pipelines to install dependencies and handle cleanup. We’ll demonstrate using the cowsay gem to generate fun ASCII artwork.

Prerequisites

  • A GitLab project with CI/CD enabled
  • Basic understanding of GitLab CI/CD YAML syntax
  • Docker runner or shared runners configured

Table of Contents

  1. Define OS Version Jobs
  2. Generate ASCII Artwork with cowsay
  3. Pipeline Failure: Missing Dependency
  4. Install Dependencies with before_script
  5. Successful Job Output
  6. Summary
  7. Links and References

Define OS Version Jobs

Create lightweight jobs to display OS versions on various runners:

windows_job:
  tags:
    - shared-windows
  script:
    - echo "Windows OS Version"
    - systeminfo

linux_job:
  tags:
    - saas-linux-medium-amd64
  script:
    - echo "Linux OS Version"
    - cat /etc/os-release

macos_job:
  tags:
    - saas-macos-medium-m1
  script:
    - echo "macOS Version"
    - system_profiler SPSoftwareDataType

Generate ASCII Artwork with cowsay

Define a pipeline that uses cowsay to output ASCII art into a file:

workflow:
  name: Generate ASCII Artwork

ascii_job:
  script:
    - echo "Generating ASCII artwork using COWSAY"
    - cowsay -f dragon "Run for cover, I am a DRAGON....RAWR" >> dragon.txt
    - grep -i "dragon" dragon.txt
    - cat dragon.txt

Pipeline Failure: Missing Dependency

Running the above job on the default Ruby image fails:

$ cowsay -f dragon "Run for cover, I am a DRAGON....RAWR" >> dragon.txt
/usr/bin/bash: line 1: cowsay: command not found
ERROR: Job failed: exit code 1

The error indicates cowsay is not preinstalled in the base image.


Install Dependencies with before_script

To ensure cowsay is available, install it via before_script:

workflow:
  name: Generate ASCII Artwork

ascii_job:
  image: ruby:3.1
  before_script:
    - gem install cowsay
  script:
    - echo "Generating ASCII artwork using COWSAY"
    - cowsay -f dragon "Run for cover, I am a DRAGON....RAWR" >> dragon.txt
    - grep -i "dragon" dragon.txt
    - cat dragon.txt
  after_script:
    - echo "Cleanup after job"
KeywordPurposeExample
before_scriptInstall dependencies or prepare environment before script- gem install cowsay
scriptMain steps of the jobASCII generation commands
after_scriptCleanup or notifications after the job- echo "Cleanup after job"

Runner Considerations

Ensure your runner can access external package sources. For air-gapped environments, pre-build a custom Docker image with required gems.


Successful Job Output

With cowsay installed, the pipeline completes successfully:

$ gem install cowsay
Successfully installed cowsay-0.3.0
1 gem installed

$ echo "Generating ASCII artwork using COWSAY"
Generating ASCII artwork using COWSAY

$ cowsay -f dragon "Run for cover, I am a DRAGON....RAWR" >> dragon.txt

$ grep -i "dragon" dragon.txt
 Run for cover, I am a DRAGON....RAWR |

$ cat dragon.txt
 ________
< Run for cover, I am a DRAGON....RAWR >
 --------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

$ echo "Cleanup after job"
Cleanup after job

Summary

By adding a before_script section to your GitLab CI/CD job, you can:

  • Automatically install required libraries or tools
  • Avoid failures due to missing dependencies
  • Leverage after_script for cleanup and notifications

This pattern ensures robust, repeatable pipelines across all environments.


Watch Video

Watch video content

Previous
Run Jobs with Shared Runners