</>

GitLab CI Cheat Sheet

Practical GitLab CI cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.

gitlab-ci gitlab ci-cd ci automation

GitLab CI cheat sheet with real commands and snippets for setup, core workflows, debugging, and production-safe automation patterns. If you are working across tools, pair this with the Pytest Cheat Sheet and Jenkins Cheat Sheet.

Setup and Installation

Goal: Verify installation and CLI metadata

# Resolve command path from current shell
command -v gitlab-runner

# Print installed version to confirm runtime
gitlab-runner --version

# Read top-level help before using subcommands
gitlab-runner --help

Goal: Install or upgrade the tool on a workstation

# Install GitLab Runner for local CI job execution
brew install gitlab-runner

# Verify runner binary version
gitlab-runner --version

Goal: Run day-to-day commands

# Lint .gitlab-ci.yml via GitLab API
curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" --form "content=<.gitlab-ci.yml" https://gitlab.com/api/v4/ci/lint

# Run GitLab Runner in shell mode
gitlab-runner exec shell test_job

# Verify gitlab-runner installation
gitlab-runner --version

# Register runner with your GitLab instance
gitlab-runner register

Core Workflows

Goal: Capture command help for quick offline lookup

# Create docs folder for generated command references
mkdir -p docs/cli

# Write help output to a timestamped file
gitlab-runner --help > docs/cli/gitlab-ci-help.txt

# Search help output for a keyword
rg "config|auth|deploy" docs/cli/gitlab-ci-help.txt

Goal: Wrap repetitive commands in a script

# Create scripts directory for local automation
mkdir -p scripts

# Write repeatable health-check script
cat > scripts/check-gitlab-ci.sh <<'SH'
#!/usr/bin/env bash
set -euo pipefail
gitlab-runner --version
gitlab-runner --help >/dev/null
SH

# Make script executable and run it
chmod +x scripts/check-gitlab-ci.sh && ./scripts/check-gitlab-ci.sh

Configuration and Environment

Goal: Pin environment variables for predictable runs

# Define environment profile for local commands
export GITLAB_CI_PROFILE=dev

# Persist profile in local shell configuration
echo 'export GITLAB_CI_PROFILE=dev' >> ~/.zshrc

# Reload shell profile changes
source ~/.zshrc

Automation and CI

Goal: Define a working CI pipeline file

stages:
  - test
  - build

test:
  stage: test
  image: node:22
  script:
    - npm ci
    - npm run check

build:
  stage: build
  image: node:22
  script:
    - npm ci
    - npm run build

Goal: Add tool checks to CI pipeline

# Run lint/test/build in strict mode
set -euo pipefail

# Verify CLI exists before invoking workflow
command -v gitlab-runner

# Exit quickly if command is unavailable
gitlab-runner --version

Debugging and Troubleshooting

Goal: Collect diagnostics when commands fail

# Capture command output and exit code
gitlab-runner --help > /tmp/gitlab-ci-debug.log 2>&1; echo $?

# Inspect captured diagnostics
tail -n 80 /tmp/gitlab-ci-debug.log

# Check current shell PATH entries
echo $PATH | tr ':' '\n'

Common Gotchas

  • Pin GitLab CI versions in CI so local and pipeline behavior match.
  • Check tool authentication context before running write or deploy commands.
  • Prefer non-interactive flags in scripts to avoid stalled jobs.
  • Capture stderr logs in CI artifacts for faster incident triage.
  • Keep secrets in environment variables, not committed scripts.

Related Cheat Sheets