name: Build for Release

on:
  release:
    types: [created]

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - arch_name: x86_64
            cc: gcc
            cflags:
          - arch_name: x86
            cc: gcc
            cflags: -m32

    steps:
      - uses: actions/checkout@v4
      - name: Build Project
        uses: ./.github/actions/build
        with:
          arch_name: ${{ matrix.arch_name }}
          cc: ${{ matrix.cc }}
          cflags: ${{ matrix.cflags }}

  upload:
    permissions: write-all
    runs-on: ubuntu-latest
    needs: build
    steps:
      - uses: actions/checkout@v4
      - name: Merge Build Artifacts
        uses: actions/upload-artifact/merge@v4
        with:
          name: all-builds
          pattern: build-*
          delete-merged: true
          retention-days: 1

      - uses: actions/download-artifact@v4
        with:
          name: all-builds
          path: builds

      - name: Upload Builds to Release
        run: |
          cd builds
          for file in ./*; do
            echo "Uploading $file..."
            gh release upload ${{ github.event.release.tag_name }} "$file" --clobber
          done
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
