Course contentsShow
← Cloud and Infrastructure
Lesson 1743 of 2,98936. GitLab CI/CDFree lesson

Job Script Commands

Writing job scripts with before_script, script, and after_script sections for task execution.

Job Script Commands

What you'll learn: How to structure the commands your GitLab CI/CD jobs execute using before_script, script, and after_script sections.

Understanding Job Script Sections

Every GitLab CI/CD job needs to know what commands to run. GitLab provides three script sections that execute in sequence, each serving a distinct purpose:

before_script

Think of this as your setup phase. Commands here run before your main task and are perfect for preparation work like installing dependencies, authenticating, or setting up environment configurations. These commands run every time, even if your main script fails.

script

This is the heart of your job — the main work you need done. It might be compiling code, running tests, or building Docker images. This section is required in every job. If any command here fails (exits with non-zero status), the entire job fails.

after_script

Your cleanup crew. These commands run after the script section completes, regardless of whether it succeeded or failed. Perfect for sending notifications, collecting logs, or performing cleanup tasks. Importantly, failures here don't affect your job's overall pass/fail status.

Execution Flow Analogy

Imagine preparing a meal: before_script gathers ingredients and preheats the oven, script does the actual cooking, and after_script cleans the kitchen — whether your dish turned out perfect or burned.

Simple Example Structure

build-app:
  before_script:
    - apt-get update
    - apt-get install -y build-essential
  script:
    - make build
    - make test
  after_script:
    - echo "Build completed"

Key Takeaway: Use before_script for setup, script for your core task (required), and after_script for cleanup that runs regardless of success or failure.