name: CI/CD SplitterMR

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - name: Build docker image for testing
        run: |
          docker build \
            -f Dockerfile.test \
            -t splitter_mr_tests \
            .
      - name: Run tests and check coverage
        run: docker run splitter_mr_tests

  deploy_docs:
    runs-on: ubuntu-latest
    needs: test
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - name: Build docker for Docs
        run: |
          docker build \
            -f Dockerfile.docs \
            -t splitter_mr_docs \
            .
      - name: Deploy docs
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: docker run -e GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} splitter_mr_docs

  publish:
    runs-on: ubuntu-latest
    needs: test
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - name: Remove old build artifacts
        run: rm -rf dist build
      - name: Install dependencies for building
        run: pip install build 'setuptools_scm[toml]' wheel
      - name: Build the package
        run: python -m build --sdist --wheel --no-isolation
      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.PYPI_API_TOKEN }}