name: Release

on:
  push:
    tags:
      - "v*"
  workflow_dispatch:
    inputs:
      tag:
        description: "Existing release tag to build and publish"
        required: true
        type: string

permissions:
  contents: write

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
          - os: macos-15-intel
            target: x86_64-apple-darwin
          - os: macos-14
            target: aarch64-apple-darwin
    steps:
      - uses: actions/checkout@v5
        with:
          ref: ${{ inputs.tag || github.ref_name }}
      - name: Add Rust target
        run: rustup target add "${{ matrix.target }}"
      - name: Build
        run: cargo build --release --locked --target "${{ matrix.target }}"
      - name: Package
        run: |
          archive="orqa-${{ matrix.target }}.tar.gz"
          mkdir -p dist
          cp "target/${{ matrix.target }}/release/orqa" dist/orqa
          tar -C dist -czf "$archive" orqa
          shasum -a 256 "$archive" > "$archive.sha256"
      - uses: actions/upload-artifact@v7
        with:
          name: orqa-${{ matrix.target }}
          path: |
            orqa-${{ matrix.target }}.tar.gz
            orqa-${{ matrix.target }}.tar.gz.sha256

  release:
    name: Publish GitHub release
    runs-on: ubuntu-latest
    needs: build
    steps:
      - uses: actions/download-artifact@v8
        with:
          merge-multiple: true
      - name: Publish release
        env:
          GH_TOKEN: ${{ github.token }}
          RELEASE_TAG: ${{ inputs.tag || github.ref_name }}
        run: |
          gh release create "$RELEASE_TAG" \
            ./*.tar.gz \
            ./*.tar.gz.sha256 \
            --repo "$GITHUB_REPOSITORY" \
            --title "orqa $RELEASE_TAG" \
            --generate-notes \
            --verify-tag
