name: Release

on:
  push:
    tags:
      - "v*" # trigger on version tags like v1.0.0

permissions: write-all

jobs:
  create-release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: taiki-e/create-gh-release-action@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

  build-and-upload-assets:
    needs: create-release
    strategy:
      matrix:
        include:
          - name: linux-amd64
            target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - name: linux-arm64
            target: aarch64-unknown-linux-gnu
            os: ubuntu-24.04-arm

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

    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      - name: Setup cross toolchain
        uses: taiki-e/setup-cross-toolchain-action@v1
        with:
          target: ${{ matrix.target }}

      - name: Install build deps
        run: |
          sudo apt-get update
          sudo apt-get install -y build-essential pkg-config libssl-dev ca-certificates libvips-tools libvips-dev

      - name: Build and upload
        uses: taiki-e/upload-rust-binary-action@v1
        with:
          bin: ${{ github.event.repository.name }}
          target: ${{ matrix.target }}
          archive: $bin-${{ matrix.name }}
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Publish crate
        if: matrix.name == 'linux-amd64'
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: cargo publish --allow-dirty
