name: Release-plz

permissions:
  pull-requests: write
  contents: write

on:
  push:
    branches:
      - master

jobs:
  release-plz:
    name: Release-plz
    runs-on: ubuntu-latest
    outputs:
      releases_created: ${{ steps.release-plz.outputs.releases_created }}
      tag: ${{ steps.extract-tag.outputs.tag }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          token: ${{ secrets.RELEASE_PLZ_TOKEN }}

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Run release-plz
        id: release-plz
        uses: MarcoIeni/release-plz-action@v0.5
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_PLZ_TOKEN }}
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

      - name: Extract tag name
        id: extract-tag
        if: steps.release-plz.outputs.releases_created == 'true'
        run: |
          TAG=$(echo '${{ steps.release-plz.outputs.releases }}' | jq -r '.[0].tag')
          echo "tag=$TAG" >> "$GITHUB_OUTPUT"

  upload-assets:
    name: Upload Binaries (${{ matrix.target }})
    needs: release-plz
    if: needs.release-plz.outputs.releases_created == 'true'
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: x86_64-pc-windows-msvc
            os: windows-latest
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Upload binaries to release
        uses: taiki-e/upload-rust-binary-action@v1
        with:
          bin: andlock
          target: ${{ matrix.target }}
          ref: refs/tags/${{ needs.release-plz.outputs.tag }}
          token: ${{ secrets.GITHUB_TOKEN }}
