name: Release

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

permissions:
  contents: write

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          # CPU builds
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            use_cross: false
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            use_cross: true
          - target: x86_64-apple-darwin
            os: macos-14
            use_cross: false
          - target: aarch64-apple-darwin
            os: macos-latest
            use_cross: false
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            use_cross: false
          # GPU builds
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            use_cross: false
            features: transcribe-cuda
            suffix: -cuda
            cuda: true
          - target: aarch64-apple-darwin
            os: macos-latest
            use_cross: false
            features: transcribe-metal
            suffix: -metal

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v4

      - name: Clone instruction-files (path dependency)
        run: git clone --depth 1 https://github.com/btakita/instruction-files.git ../instruction-files

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

      - name: Install CUDA toolkit
        if: matrix.cuda
        uses: Jimver/cuda-toolkit@v0.2.18
        with:
          cuda: '12.6.3'
          method: network
          sub-packages: '["nvcc", "cudart"]'

      - name: Install cross
        if: matrix.use_cross
        run: cargo install cross --version 0.2.5

      - name: Build (cross)
        if: matrix.use_cross
        run: cross build --release --target ${{ matrix.target }} --features vendored-openssl
        env:
          CROSS_CONTAINER_ENGINE: docker

      - name: Build (cargo)
        if: "!matrix.use_cross"
        run: cargo build --release --target ${{ matrix.target }} ${{ matrix.features && format('--features {0}', matrix.features) || '' }}

      - name: Package (Unix)
        if: runner.os != 'Windows'
        run: tar czf corky-${{ matrix.target }}${{ matrix.suffix || '' }}.tar.gz -C target/${{ matrix.target }}/release corky

      - name: Package (Windows)
        if: runner.os == 'Windows'
        shell: pwsh
        run: Compress-Archive -Path target/${{ matrix.target }}/release/corky.exe -DestinationPath corky-${{ matrix.target }}${{ matrix.suffix || '' }}.zip

      - name: Upload artifact (Unix)
        if: runner.os != 'Windows'
        uses: actions/upload-artifact@v4
        with:
          name: corky-${{ matrix.target }}${{ matrix.suffix || '' }}
          path: corky-${{ matrix.target }}${{ matrix.suffix || '' }}.tar.gz

      - name: Upload artifact (Windows)
        if: runner.os == 'Windows'
        uses: actions/upload-artifact@v4
        with:
          name: corky-${{ matrix.target }}${{ matrix.suffix || '' }}
          path: corky-${{ matrix.target }}${{ matrix.suffix || '' }}.zip

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

      - name: Create release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GH_REPO: ${{ github.repository }}
        run: |
          tag="${GITHUB_REF#refs/tags/}"
          gh release create "$tag" \
            --title "corky $tag" \
            --generate-notes \
            artifacts/*
