name: "Build release"

on:
  push:
    tags:
      - app/v*.*.*

jobs:
  build:
    uses: ./.github/workflows/build-common.yml

  publish:
    name: Publish
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Check out
        uses: actions/checkout@v7

      - name: Get version
        id: get_version
        run: echo "version=$(git describe --tags --always --match 'app/v*' | sed -n 's|app/\([^/-]*\)\(-.*\)\{0,1\}|\1|p')" >> $GITHUB_OUTPUT

      - name: Setup uv
        uses: astral-sh/setup-uv@v7
        with:
          enable-cache: true

      - name: Download build
        uses: actions/download-artifact@v8
        with:
          name: build-temp-${{ github.sha }}
          path: build

      - name: Upload GitHub
        uses: softprops/action-gh-release@v3
        with:
          files: build/*

      - name: Upload CF bucket
        uses: shallwefootball/upload-s3-action@v1.3.3
        with:
          aws_key_id: ${{ secrets.CF_KEY_ID }}
          aws_secret_access_key: ${{ secrets.CF_KEY }}
          aws_bucket: "hydownload"
          endpoint: "https://bea223c61d5a41250d127bd67f51dfec.r2.cloudflarestorage.com/"
          source_dir: "build"
          destination_dir: "app/${{ steps.get_version.outputs.version }}"

      - name: Publish to API
        run: |
          export HY_API_POST_KEY=${{ secrets.HY2_API_POST_KEY }}
          uv run python hyperbole.py publish
