name: Release

on:
  push:
    tags:
      - "*"

env:
  DOCKER_REGISTRY: ghcr.io
  DOCKER_IMAGE_NAME: ${{ github.repository }}

jobs:
  release:
    name: release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Python 3.x
        uses: actions/setup-python@v4
        with:
          python-version: "3.x"

      - name: Install release dependencies
        run: |
          python -m pip install --upgrade pip
          pip install setuptools wheel twine

      - name: Build and publish package
        env:
          TWINE_USERNAME: ${{ secrets.PYPI_STACUTILS_USERNAME }}
          TWINE_PASSWORD: ${{ secrets.PYPI_STACUTILS_PASSWORD }}
        run: |
          scripts/publish
  docker:
    name: docker
    permissions:
      contents: read
      packages: write
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.DOCKER_REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Docker meta main
        id: meta-main
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_IMAGE_NAME }}
          flavor: |
            latest=true
          tags: |
            type=pep440,pattern={{version}}
      - name: Build and push main
        uses: docker/build-push-action@v5
        with:
          context: .
          file: docker/Dockerfile
          target: main
          push: true
          tags: ${{ steps.meta-main.outputs.tags }}
          labels: ${{ steps.meta-main.outputs.labels }}
      - name: Docker meta dev
        id: meta-dev
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_IMAGE_NAME }}
          flavor: |
            latest=true
            suffix=-dev,onlatest=true
          tags: |
            type=pep440,pattern={{version}}
      - name: Build and push dev
        uses: docker/build-push-action@v5
        with:
          context: .
          file: docker/Dockerfile
          target: dev
          push: true
          tags: ${{ steps.meta-dev.outputs.tags }}
          labels: ${{ steps.meta-dev.outputs.labels }}
