ci: add GitLab CI/CD pipeline for linting, testing, and publishing

This commit is contained in:
2026-02-04 01:09:47 +00:00
parent a46e216902
commit 3e026866cb

123
.gitlab-ci.yml Normal file
View File

@@ -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