# To run the release workflow push a tag with the expected SHA256SUMS as tag message body.

name: Release

on:
  push:
    tags: '*'
jobs:
  build:
    runs-on: ubuntu-24.04
    permissions: read-all

    steps:
      - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0

      - name: Sanity checks
        run: |
          # GHA makes the tag point to the commit rather than the tag object.
          # Remove the tag and fetch it again to get the real tag object.
          git tag -d "$(echo "$GITHUB_REF" | sed 's/refs\/tags\///')"
          git fetch https://github.com/trifectatechfoundation/sudo-rs.git --tags

          # Check if the tag has a signature to prevent accidentally pushing an unsigned tag.
          git tag -l --format='%(contents:signature)' "$(echo "$GITHUB_REF" | sed 's/refs\/tags\///')" | grep --quiet SIGNATURE || (echo "Tag not signed"; exit 1)

      - name: Run build
        run: ./util/build-release.sh

      # Upload the built tarballs first before comparing checksums to help with debugging.
      - name: Upload artifacts
        uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
        with:
          name: release_files
          path: |
            target/pkg/SHA256SUMS
            target/pkg/*.tar.gz

      - name: Compare checksums
        run: |
          # Get the expected checksums from the tag message.
          git tag -l --format='%(contents:body)' "$(echo "$GITHUB_REF" | sed 's/refs\/tags\///')" | tr -s '\n' > expected_checksums.txt

          # Check that the actual checksums match what we expected. If not fail
          # the release and have the person doing the release check again for
          # reproducibility problems.
          cat expected_checksums.txt
          diff -u expected_checksums.txt target/pkg/SHA256SUMS

  release:
    runs-on: ubuntu-24.04
    permissions:
       contents: write
    needs: build

    steps:
      - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0

      - name: Download artifacts
        uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
        with:
          name: release_files
          path: release_files

      - name: Prepare release
        run: |
          echo "Release files:"
          ls -l release_files
          echo

          # Extract the first changelog entry from CHANGELOG.md
          echo "Changelog:"
          sed -n '4,${ /^## /q; p; }' CHANGELOG.md | tee changes.md

      - name: Create release
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          # GHA makes the tag point to the commit rather than the tag object.
          # Remove the tag and fetch it again to get the real tag object.
          RELEASE="$(echo "$GITHUB_REF" | sed 's/refs\/tags\///')"
          git tag -d "$RELEASE"
          git fetch https://github.com/trifectatechfoundation/sudo-rs.git --tags

          gh release create "$RELEASE" --draft \
            --title "Version ${RELEASE#v}" \
            --notes-file changes.md release_files/* \
            --verify-tag
          echo "Draft release successfully created. Please review and publish."
