name: Release

on:
  push:
    tags:
      - "2.0.0-rc.*"

permissions:
  contents: write

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - goos: linux
            goarch: amd64
          - goos: linux
            goarch: arm64
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

      - uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6.5.0
        with:
          go-version-file: go.mod

      - name: Build
        env:
          GOOS: ${{ matrix.goos }}
          GOARCH: ${{ matrix.goarch }}
        run: |
          make build

      - name: Package
        run: |
          cd ./bin
          tar czf ../distrobox-${{ matrix.goos }}-${{ matrix.goarch }}.tar.gz .

      - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: distrobox-${{ matrix.goos }}-${{ matrix.goarch }}
          path: distrobox-${{ matrix.goos }}-${{ matrix.goarch }}.tar.gz

  release:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

      - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          path: artifacts
          merge-multiple: true

      - name: Create Release
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          version="${GITHUB_REF_NAME}"
          # If a release for this tag already exists (e.g. it was drafted
          # from the web UI, which pushes the tag along with the draft),
          # delete it so we can re-create it with the freshly built artifacts
          # and auto-generated notes. The tag itself is left untouched.
          if gh release view "${version}" >/dev/null 2>&1; then
            gh release delete "${version}" --yes
          fi
          gh release create "${version}" \
            --title "${version}" \
            --generate-notes \
            --prerelease \
            artifacts/*
