Presets / Python : lint + tests + build + deploy
preset Python

Python : lint + tests + build + deploy

gitlab-ci  ·  Python

Pipeline GitLab avec stages lint/test/build Docker/deploy, cache pip et artefacts coverage.

python pytest docker deploy
stages:
  - lint
  - test
  - build
  - deploy

variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
  IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA

cache:
  key: "$CI_COMMIT_REF_SLUG"
  paths:
    - .cache/pip
    - .venv/

.python:
  image: python:3.12
  before_script:
    - python -m venv .venv
    - source .venv/bin/activate
    - pip install -r requirements.txt -r requirements-dev.txt

lint:
  extends: .python
  stage: lint
  script:
    - ruff check .
    - black --check .

test:
  extends: .python
  stage: test
  script:
    - pytest --cov=. --cov-report=xml --junitxml=report.xml
  artifacts:
    when: always
    reports:
      junit: report.xml
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml

build:
  stage: build
  image: docker:27
  services:
    - docker:27-dind
  script:
    - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin $CI_REGISTRY
    - docker build -t $IMAGE .
    - docker push $IMAGE
  only:
    - main
    - tags

deploy:
  stage: deploy
  image: alpine/k8s:1.30.0
  script:
    - kubectl config use-context $KUBE_CONTEXT
    - kubectl set image deployment/app app=$IMAGE
    - kubectl rollout status deployment/app
  environment:
    name: production
  only:
    - main