name: Rust CI and Release

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

permissions:
  contents: read

jobs:
  ci:
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/heads/main')
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false
      - uses: dtolnay/rust-toolchain@29eef336d9b2848a0b548edc03f92a220660cdb8 # stable
      - name: Format
        run: cargo fmt --check
      - name: Clippy
        run: cargo clippy --all-targets --locked -- -D warnings
      - name: Test
        run: cargo test --locked
      - name: Package crate
        run: cargo package --locked

  release:
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') && github.repository_owner == 'wagov-dtt'
    name: Release (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    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@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false
      - uses: dtolnay/rust-toolchain@29eef336d9b2848a0b548edc03f92a220660cdb8 # stable
        with:
          targets: ${{ 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@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        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.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') && 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@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
        with:
          path: dist
          merge-multiple: true
      - name: Attest release assets
        uses: actions/attest-build-provenance@c074443f1aee8d4aeeae555aebba3282517141b2 # v2.2.3
        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.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') && github.repository_owner == 'wagov-dtt'
    needs: release
    runs-on: ubuntu-latest
    environment: crates-io
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false
      - uses: dtolnay/rust-toolchain@29eef336d9b2848a0b548edc03f92a220660cdb8 # stable
      - 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
