name: Publish

on:
  push:
    branches:
      - master
    tags:
      - "v*.*.*"
  pull_request:

jobs:
  publish:
    name: Publish for ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            artifact_name: oha
            release_name: oha-linux-amd64
            target: x86_64-unknown-linux-musl
            additional_args: "--features vsock"
            use_cross: false
          - os: windows-latest
            artifact_name: oha.exe
            release_name: oha-windows-amd64.exe
            target: x86_64-pc-windows-msvc
            additional_args: ""
            use_cross: false
          - os: macos-latest
            artifact_name: oha
            release_name: oha-macos-amd64
            target: x86_64-apple-darwin
            additional_args: ""
            use_cross: false
          - os: ubuntu-latest
            artifact_name: oha
            release_name: oha-linux-arm64
            target: aarch64-unknown-linux-musl
            additional_args: "--features vsock"
            use_cross: true
          - os: macos-14
            artifact_name: oha
            release_name: oha-macos-arm64
            target: aarch64-apple-darwin
            additional_args: ""
            use_cross: false
    env:
      BUILD_CMD: cargo
    steps:
      - uses: ilammy/setup-nasm@v1
      - uses: actions/checkout@v6
      - name: Install musl-tools on Linux
        run: sudo apt-get update --yes && sudo apt-get install --yes musl-tools
        if: contains(matrix.target, 'musl')
      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      - name: Install cross
        if: matrix.use_cross
        uses: taiki-e/install-action@v2
        with:
          tool: cross
      - uses: Swatinem/rust-cache@v2
      - name: Overwrite build command env variable
        if: matrix.use_cross
        shell: bash
        run: echo "BUILD_CMD=cross" >> $GITHUB_ENV
      - name: Build
        shell: bash
        run: $BUILD_CMD build --profile release-ci --target ${{ matrix.target }} --locked --no-default-features --features rustls ${{ matrix.additional_args }}
      - name: Upload
        uses: actions/upload-artifact@v5
        with:
          name: ${{ matrix.release_name }}
          path: target/${{ matrix.target }}/release-ci/${{ matrix.artifact_name }}
      - name: Upload binaries to release
        if: startsWith(github.ref, 'refs/tags/v')
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: target/${{ matrix.target }}/release-ci/${{ matrix.artifact_name }}
          asset_name: ${{ matrix.release_name }}
          tag: ${{ github.ref }}
