name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  build:
    name: Build ${{ matrix.name }}
    runs-on: ${{ matrix.runner }}
    permissions:
      contents: write
    strategy:
      matrix:
        include:
          - name: linux-amd64
            runner: ubuntu-latest
            target: x86_64-unknown-linux-musl
            artifact: dpdk-top-linux-amd64
          - name: linux-arm64
            runner: ubuntu-24.04-arm
            target: aarch64-unknown-linux-musl
            artifact: dpdk-top-linux-arm64
          - name: macos-amd64
            runner: macos-latest
            target: x86_64-apple-darwin
            artifact: dpdk-top-darwin-amd64
          - name: macos-arm64
            runner: macos-latest
            target: aarch64-apple-darwin
            artifact: dpdk-top-darwin-arm64

    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Install musl tools
        if: contains(matrix.target, 'musl')
        run: sudo apt-get update && sudo apt-get install -y musl-tools

      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}

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

      - name: Package
        run: |
          mkdir -p dist
          cp target/${{ matrix.target }}/release/dpdk-top dist/
          cd dist
          tar czf ../${{ matrix.artifact }}.tar.gz dpdk-top

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact }}
          path: ${{ matrix.artifact }}.tar.gz

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4

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

      - name: Generate checksums
        run: |
          cd artifacts
          find . -name '*.tar.gz' -exec mv {} . \;
          sha256sum *.tar.gz > checksums.txt
          cat checksums.txt

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: |
            artifacts/*.tar.gz
            artifacts/checksums.txt
