Skip to content

9. GitLab CI comparison ​

Home · Full index

Full workflow files in this section belong in the repository root as .gitlab-ci.yml. The snippets assume a GitLab runner with a Docker or Kubernetes executor for the specified images and services.

9.1 X01 · Lint and test in parallel, then package ​

GitLab CI · Full pipeline. Requires requirements-dev.txt containing Ruff, pytest and build, plus a buildable Python project. Jobs in the first stage can run concurrently.

yaml
stages: [verify, package]
default:
  image: python:3.12-slim

lint:
  stage: verify
  script:
    - python -m pip install -r requirements-dev.txt
    - python -m ruff check .

test:
  stage: verify
  script:
    - python -m pip install -r requirements-dev.txt
    - python -m pytest -q

package:
  stage: package
  needs: [lint, test]
  script:
    - python -m pip install -r requirements-dev.txt
    - python -m build --wheel
    - ls -l dist/*.whl
  artifacts:
    paths: [dist/*.whl]
    expire_in: 1 week

Verify. Fail the lint job and check the package job, then restore lint and download the wheel.

9.2 X02 · Switch between branch and merge-request pipelines ​

GitLab CI · Top-level fragment. This limits the example to merge requests and branch pushes. Scheduled and tag pipelines need their own rules if your project uses them.

yaml
workflow:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS'
      when: never
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH'

Verify. Push to a feature branch before and after opening an MR. Compare the pipeline sources and check there is one pipeline per push.

9.3 X03 · Cache pip downloads and collect JUnit results ​

GitLab CI · Full pipeline. Requires a committed requirements-dev.txt with pytest. It uses a single Python image. Include the runtime in the cache namespace if expanding to a matrix.

yaml
stages: [test]

test:
  image: python:3.12-slim
  stage: test
  variables:
    PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
  cache:
    key:
      prefix: py312
      files: [requirements-dev.txt]
    paths: [.cache/pip/]
  script:
    - python -m pip install -r requirements-dev.txt
    - python -m pytest -q --junitxml=junit.xml
  artifacts:
    when: always
    reports:
      junit: junit.xml
    paths: [junit.xml]
    expire_in: 1 week

Verify. Compare a warm run and a cold run and inspect the MR test report after a failure. The failure should remain visible even when the report uploads.

9.4 X04 · Consume a wheel artifact through needs ​

GitLab CI · Job fragment. Add to X01. Append inspect to its stages list. Replace calculator with the module your wheel provides. This import check assumes no third-party runtime dependencies.

yaml
inspect:
  stage: inspect
  needs:
    - job: package
      artifacts: true
  script:
    - python -m venv .wheel-venv
    - .wheel-venv/bin/python -m pip install --no-deps dist/*.whl
    - .wheel-venv/bin/python -I -c 'import calculator'

Verify. The inspect job receives the same wheel from the package job. Renaming the artifact path on a scratch branch should make the install step fail.

9.5 X05 · Run parallel Python versions ​

GitLab CI · Full pipeline. Requires pytest in a committed requirements-dev.txt.

yaml
stages: [test]

test:
  stage: test
  image: "python:${PYTHON_VERSION}-slim"
  parallel:
    matrix:
      - PYTHON_VERSION: ['3.11', '3.12', '3.13']
  script:
    - python -m pip install -r requirements-dev.txt
    - python -m pytest -q

Verify. The pipeline shows three jobs, one for each matrix value. Actual overlap depends on runner capacity.

Reference · GitLab YAML reference, workflow rules, CI Lint.