---
name: "Tag & Release management"
on:
  release:
    types:
      - published

jobs:
  pypi:
    name: Publish Python 🐍 distribution 📦 to PyPI
    runs-on: ubuntu-latest
    environment:
      name: production
      url: https://pypi.org/p/anta
    permissions:
      id-token: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install setuptools wheel build
      - name: Build package
        run: |
          python -m build
      - name: Publish distribution 📦 to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1

  release-coverage:
    name: Updated ANTA release coverage badge
    runs-on: ubuntu-20.04
    needs: [pypi]
    steps:
      - uses: actions/checkout@v4
      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"
      - name: Install dependencies
        run: pip install genbadge[coverage] tox tox-gh-actions
      - name: "Run pytest via tox for ${{ matrix.python }}"
        run: tox
      - name: Generate coverage badge
        run: genbadge coverage -i .coverage.xml -o badge/latest-release-coverage.svg
      - name: Publish coverage badge to gh-pages branch
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          branch: coverage-badge
          folder: badge
  release-doc:
    name: "Publish documentation for release ${{github.ref_name}}"
    runs-on: ubuntu-latest
    needs: [release-coverage]
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: 'Setup Python 3 on runner'
        uses: actions/setup-python@v5
        with:
          python-version: '3.x'
      - name: Setup Git config
        run: |
          git config --global user.name 'github-actions[bot]'
          git config --global user.email 'github-actions[bot]@users.noreply.github.com'

      - name: 'Build mkdocs content to site folder'
        run: |
          pip install .[doc]
          mike deploy --update-alias --push  ${{github.ref_name}} stable

  docker:
    name: Docker Image Build
    runs-on: ubuntu-latest
    needs: [pypi]
    strategy:
      matrix:
        platform:
          - linux/amd64
          - linux/arm64
          - linux/arm/v7
          - linux/arm/v8
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Docker meta for TAG
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ghcr.io/${{ github.repository }}
          tags: |
            type=semver,pattern={{version}}
            type=raw,value=latest

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: .
          file: Dockerfile
          push: true
          platforms: linux/amd64
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
