name: Release

on:
  push:
    tags: ["v*"]

permissions:
  contents: write

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        target:
          - x86_64-unknown-linux-gnu
          - aarch64-unknown-linux-gnu
    steps:
      - uses: actions/checkout@v7

      - name: Install cross and cargo-deb
        run: |
          cargo install cross --git https://github.com/cross-rs/cross
          cargo install cargo-deb

      - name: Build
        run: cross build --release --target ${{ matrix.target }}

      - name: Package tarball
        run: |
          tar czf rdmatop-${{ github.ref_name }}-${{ matrix.target }}.tar.gz \
            -C target/${{ matrix.target }}/release rdmatop

      - name: Package deb
        run: cargo deb --no-build --no-strip --target ${{ matrix.target }}

      - name: Upload artifact
        uses: actions/upload-artifact@v7
        with:
          name: rdmatop-${{ matrix.target }}
          path: |
            rdmatop-*.tar.gz
            target/${{ matrix.target }}/debian/*.deb

  release:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v8
        with:
          merge-multiple: true

      - name: Create release
        uses: softprops/action-gh-release@v3
        with:
          files: |
            rdmatop-*.tar.gz
            **/*.deb
          generate_release_notes: true
