Gitlab CI Reusable Jobs - Setup and Configuration

Learn how to efficiently manage Gitlab CI jobs using reusable YAML configurations. This guide demonstrates reusable jobs for setting up Python development environments, including virtual environments and linting.

Gitlab CI Reusable Jobs

This document demonstrates the use of reusable jobs in Gitlab CI for setting up Python development environments.

Reusable Python Setup Job

The .python-requirements job defines a reusable template for setting up a Python virtual environment and installing dependencies.

.python-requirements:
  script:
    - python3 -m venv venv
    - source venv/bin/activate
    - pip install -r requirements.txt
    - pip install pylint

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - venv
    - .cache/pip

Development and Staging Setup Jobs

The setup:dev and setup:staging jobs extend the .python-requirements job to perform environment-specific tasks.

setup:dev:
  extends: .python-requirements
  stage: setup
  image: python:3.8
  script:
    - python -V
    - echo "[SETUP DEV]"
    - pushd dev
    - !reference [.python-requirements, script]
    - deactivate
    - popd
  rules:
    - if: '$CI_COMMIT_BRANCH'

setup:staging:
  extends: .python-requirements
  stage: setup
  image: python:3.8
  script:
    - python -V
    - echo "[SETUP STAGING]"
    - pushd staging
    - !reference [.python-requirements, script]
    - pylint --version
    - deactivate
    - popd
    - echo "some other stuff"
  rules:
    - if: '$CI_COMMIT_BRANCH'

Job Reusability and Efficiency

By using reusable jobs, we avoid code duplication and improve maintainability. Changes to the Python setup process only need to be made in one place (.python-requirements).

Further Enhancements

This example can be extended to include additional steps, such as testing, deployment, and other environment-specific configurations.

Consider adding more detailed logging and error handling for improved debugging and monitoring.