name: Publish release files for non-cd-native environments

on:
  # workflow_run:
  #   workflows: ["Check SemVer compliance"]
  #   types:
  #     - completed
  release:
    types: [ created ]

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        target: [ 
          "aarch64-unknown-linux-gnu", 
          "armv7-unknown-linux-gnueabihf", 
          "x86_64-unknown-linux-musl", 
          "aarch64-unknown-linux-musl", 
          "x86_64-unknown-freebsd",  
        ]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install cargo-deb cross compilation dependencies
        run: sudo apt-get install libc6-arm64-cross libgcc-s1-arm64-cross
        if: ${{ matrix.target == 'aarch64-unknown-linux-gnu' }}
        shell: bash

      - name: Install cargo-deb
        run: cargo install cargo-deb
        if: ${{ matrix.target == 'aarch64-unknown-linux-gnu' }}
        shell: bash

      - name: install targets
        run: rustup target add ${{ matrix.target }}

      - name: install cross
        uses: taiki-e/install-action@v2
        with:
          tool: cross@0.2.5

      - name: Check format
        run: cross fmt --all -- --check

      - name: Run clippy
        run: cross clippy --all-targets --locked --target ${{matrix.target}} -- -D warnings

      - name: Run clippy (All features)
        run: cross clippy  --locked --all-features --target ${{matrix.target}} -- -D warnings

      - name: Run tests
        run: cross test --target ${{matrix.target}}

      - name: Build in Release profile with all features enabled
        run: cross build --release --all-features --target ${{matrix.target}}

      - name: Rename Release
        run: |
          mkdir -p assets
          FILENAME=topgrade-${{github.event.release.tag_name}}-${{matrix.target}}
          mv target/${{matrix.target}}/release/topgrade assets
          cd assets
          tar --format=ustar -czf $FILENAME.tar.gz topgrade
          rm topgrade
          ls .

      - name: Build Debian-based system package without autoupdate feature
        # First remove the binary built by previous steps
        # because we don't want the auto-update feature,
        # then build the new binary without auto-updating.
        run: |
          rm -rf target/${{matrix.target}}
          cross build --release --target ${{matrix.target}}
          cargo deb --target=${{matrix.target}} --no-build --no-strip
        if: ${{ matrix.target == 'aarch64-unknown-linux-gnu' }}
        shell: bash

      - name: Move Debian-based system package
        run: |
          mkdir -p assets
          mv target/${{matrix.target}}/debian/*.deb assets
        if: ${{ matrix.target == 'aarch64-unknown-linux-gnu' }}
        shell: bash

      - name: Release
        uses: softprops/action-gh-release@v2
        with:
          files: assets/*
