name: Release

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

permissions:
  contents: read

jobs:
  release:
    if: github.repository_owner == 'wagov-dtt'
    name: Release (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    permissions:
      contents: read

    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            archive: tar.gz
          - os: ubuntu-24.04-arm
            target: aarch64-unknown-linux-gnu
            archive: tar.gz
          - os: macos-14
            target: aarch64-apple-darwin
            archive: tar.gz
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
      - uses: jdx/mise-action@v4
      - name: Add cross-compilation target
        run: rustup target add ${{ matrix.target }}
      - name: Build size-optimized release binary
        env:
          RUSTFLAGS: -C debuginfo=0
        run: cargo build --release --locked --target ${{ matrix.target }}
      - name: Strip binary
        shell: bash
        run: |
          if [[ "${{ runner.os }}" == "macOS" ]]; then
            strip target/${{ matrix.target }}/release/oy
          else
            strip --strip-all target/${{ matrix.target }}/release/oy
          fi
      - name: Package
        shell: bash
        env:
          REF_NAME: ${{ github.ref_name }}
        run: |
          mkdir -p dist
          cp target/${{ matrix.target }}/release/oy dist/oy
          tar -C dist -czf "oy-${REF_NAME}-${{ matrix.target }}.tar.gz" oy
      - name: Upload artifact
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: oy-${{ github.ref_name }}-${{ matrix.target }}
          path: oy-${{ github.ref_name }}-${{ matrix.target }}.${{ matrix.archive }}
          if-no-files-found: error

  publish-release:
    if: github.repository_owner == 'wagov-dtt'
    needs: release
    runs-on: ubuntu-latest
    permissions:
      contents: write
      id-token: write
      attestations: write

    steps:
      - name: Download artifacts
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          path: dist
          merge-multiple: true
      - name: Attest release assets
        uses: actions/attest-build-provenance@a2bbfa25375fe432b6a289bc6b6cd05ecd0c4c32 # v4.1.0
        with:
          subject-path: dist/*
      - name: Create draft GitHub release with assets
        env:
          GH_TOKEN: ${{ github.token }}
          REF_NAME: ${{ github.ref_name }}
          REPOSITORY: ${{ github.repository }}
          SHA: ${{ github.sha }}
        run: |
          gh release create "${REF_NAME}" dist/* \
            --repo "${REPOSITORY}" \
            --draft \
            --target "${SHA}" \
            --title "${REF_NAME}" \
            --notes "Rust release ${REF_NAME}."
      - name: Publish GitHub release
        env:
          GH_TOKEN: ${{ github.token }}
          REF_NAME: ${{ github.ref_name }}
          REPOSITORY: ${{ github.repository }}
        run: gh release edit "${REF_NAME}" --repo "${REPOSITORY}" --draft=false

  publish-crate:
    if: github.repository_owner == 'wagov-dtt'
    needs: release
    runs-on: ubuntu-latest
    environment: crates-io
    permissions:
      contents: read
      id-token: write

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
      - uses: jdx/mise-action@v4
      - name: Verify crate package
        run: cargo package --locked
      - name: Authenticate with crates.io
        id: auth
        uses: rust-lang/crates-io-auth-action@bbd81622f20ce9e2dd9622e3218b975523e45bbe # v1.0.4
      - name: Publish crate to crates.io
        env:
          CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
        run: cargo publish --locked
