From 3e026866cb9f49d00dca657bb104ddccea4ce3ed Mon Sep 17 00:00:00 2001 From: zanewalker Date: Wed, 4 Feb 2026 01:09:47 +0000 Subject: [PATCH] ci: add GitLab CI/CD pipeline for linting, testing, and publishing --- .gitlab-ci.yml | 123 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 .gitlab-ci.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..151a2ec --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,123 @@ +stages: + - lint + - test + - build + - publish + +variables: + PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip" + UV_CACHE_DIR: "$CI_PROJECT_DIR/.cache/uv" + +cache: + paths: + - .cache/pip + - .cache/uv + - .venv + +.python-base: + image: python:3.12-slim + before_script: + - pip install uv + - uv venv + - source .venv/bin/activate + - uv sync --dev + +# Linting stage +ruff-lint: + extends: .python-base + stage: lint + script: + - uv run ruff check . + rules: + - if: $CI_PIPELINE_SOURCE == "merge_request_event" + - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH + +ruff-format: + extends: .python-base + stage: lint + script: + - uv run ruff format --check . + rules: + - if: $CI_PIPELINE_SOURCE == "merge_request_event" + - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH + +pyright: + extends: .python-base + stage: lint + script: + - uv run pyright + rules: + - if: $CI_PIPELINE_SOURCE == "merge_request_event" + - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH + +# Test stage - run tests on multiple Python versions +.test-base: + stage: test + before_script: + - pip install uv + - uv venv + - source .venv/bin/activate + - uv sync --dev + script: + - uv run pytest --cov=fastapi_traffic --cov-report=xml --cov-report=term + coverage: '/TOTAL.*\s+(\d+%)/' + artifacts: + reports: + coverage_report: + coverage_format: cobertura + path: coverage.xml + when: always + rules: + - if: $CI_PIPELINE_SOURCE == "merge_request_event" + - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH + +test-py310: + extends: .test-base + image: python:3.10-slim + +test-py311: + extends: .test-base + image: python:3.11-slim + +test-py312: + extends: .test-base + image: python:3.12-slim + +# Build stage +build-package: + extends: .python-base + stage: build + script: + - uv build + artifacts: + paths: + - dist/ + expire_in: 1 week + rules: + - if: $CI_COMMIT_TAG + - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH + +# Publish to PyPI (only on tags) +publish-pypi: + extends: .python-base + stage: publish + script: + - uv publish --token $PYPI_TOKEN + rules: + - if: $CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+$/ + when: manual + needs: + - build-package + +# Publish to GitLab Package Registry +publish-gitlab: + extends: .python-base + stage: publish + script: + - uv build + - pip install twine + - TWINE_PASSWORD=${CI_JOB_TOKEN} TWINE_USERNAME=gitlab-ci-token twine upload --repository-url ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/pypi dist/* + rules: + - if: $CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+$/ + needs: + - build-package