name: Release CI

on:
  push:
    tags:
      - "*"

jobs:
  release:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: write
    steps:
      - uses: actions/checkout@v3

      - name: Install poetry
        run: pipx install poetry
        shell: bash

      - uses: actions/setup-python@v4
        with:
          python-version: '3.11'
          cache: "poetry"

      - run: poetry install
        shell: bash

      - name: Get Version
        id: version
        run: |
          echo "VERSION=$(poetry version -s)" >> $GITHUB_OUTPUT
          echo "TAG_VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
          echo "TAG_NAME=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT

      - name: Check Version
        if: steps.version.outputs.VERSION != steps.version.outputs.TAG_VERSION
        run: exit 1

      - name: Build Package
        run: poetry build

      - name: Publish Package to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1

      - name: Publish Package to GitHub Release
        run: gh release create ${{ steps.version.outputs.TAG_NAME }} dist/*.tar.gz dist/*.whl -t "🔖 ${{ steps.version.outputs.TAG_NAME }}" --generate-notes
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
