Pipeline recipes

Лев Коваленко — Senior DS Engineer

Via pip

stages:
  - linting
  - testing
  - publish

variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

default:
  image: python:3.11
  before_script:
    - pip install -r requirements.txt
  cache:
    key: dependecies-$CI_COMMIT_BRANCH
    paths: 
      - .cache/pip

flake8-running:
  stage: linting
  script:
  - flake8 .

mypy-running:
  stage: linting
  script:
  - mypy .

pytest-running:
  stage: testing
  script:
  - pytest .

Via Poetry

stages:
  - linting
  - testing
  - publish

default:
  image: python:3.11-slim
  before_script:
    - pip install poetry
    - poetry config --local cache-dir $PWD/.poetry/cache
    - poetry install --with dev
  cache:
    key: dependecies-$CI_COMMIT_BRANCH
    paths:
      - .poetry

flake8-running:
  stage: linting
  script:
  - poerty run flake8 .

mypy-running:
  stage: linting
  script:
  - poerty run mypy .

pytest-running:
  stage: testing
  script:
  - poerty run pytest .

build-release:
  stage: build
  rules:
    - if: '$CI_COMMIT_TAG =~ /^v[0-9]+(\.[0-9]+)*$/'
  script:
    - echo "Building to python package."
    - poetry build
    - echo "Publish package to gitlab pypi."
    - poetry config repositories.gitlab "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/packages/pypi"
    - poetry config http-basic.gitlab gitlab-ci-token "$CI_JOB_TOKEN"
    - poetry publish --repository gitlab
  artifacts:
    expire_in: never
    paths:
      - $CI_PROJECT_DIR/dist

Via mamba

default:
  image: mambaorg/micromamba
  before_script:
    - micromamba create -f env.yml

flake8-running:
  stage: linting
  script:
  - micromamba run -n <venv>  flake8 .

mypy-running:
  stage: linting
  script:
  - micromamba run -n <venv>  mypy .

pytest-running:
  stage: testing
  script:
  - micromamba run -n <venv>  pytest .

mamba + poetry/pdm

default:
  image: mambaorg/micromamba
  before_script:
    - micromamba create -f env.yml
    - micromamba run -n <venv> poetry config --local cache-dir $PWD/.poetry/cache
    - micromamba run -n <venv> poetry install
  cache:
    key: dependecies-$CI_COMMIT_BRANCH
    paths:
      - .poetry

flake8-running:
  stage: linting
  script:
  - micromamba run -n <venv> poerty run flake8 .

mypy-running:
  stage: linting
  script:
  - micromamba run -n <venv> poerty run mypy .

pytest-running:
  stage: testing
  script:
  - micromamba run -n <venv> poerty run pytest .

build-release:
  stage: build
  rules:
    - if: '$CI_COMMIT_TAG =~ /^v[0-9]+(\.[0-9]+)*$/'
  script:
    - echo "Building to python package."
    - micromamba run -n <venv> poetry build
    - echo "Publish package to gitlab pypi."
    - micromamba run -n <venv> poetry config repositories.gitlab "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/packages/pypi"
    - micromamba run -n <venv> poetry config http-basic.gitlab gitlab-ci-token "$CI_JOB_TOKEN"
    - micromamba run -n <venv> poetry publish --repository gitlab
  artifacts:
    expire_in: never
    paths:
      - $CI_PROJECT_DIR/dist

DinD

stages:
  - docker

services:
  - docker:24.0.5-dind

variables:
  DOCKER_TLS_CERTDIR: "/certs"
    
build:
  image: docker:24.0.5
  stage: docker
  only:
    changes:
      - poetry.lock
      - pyproject.toml
      - Dockerfile
      - .gitlab-ci.yml
  before_script:
    - docker info
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - docker build -t $CI_REGISTRY/<group name>/<project name>:latest .
    - docker push $CI_REGISTRY/<group name>/<project name>:latest