Skip to main content
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.
  • 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:

Generate ASCII Artwork with cowsay

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

Pipeline Failure: Missing Dependency

Running the above job on the default Ruby image fails:
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:
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:

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