name: Release

on:
  push:
    tags:
      - '[0-9]+.[0-9]+.[0-9]+'
      - '[0-9]+.[0-9]+.[0-9]+-*'

permissions:
  contents: write
  packages: write

jobs:
  read-msrv:
    uses: actions-rust-lang/msrv/.github/workflows/msrv.yml@8b553824444060021f2843d7b4d803f3624d15e5 # v0.1.0

  release:
    needs: read-msrv
    runs-on: ubuntu-latest
    steps:
      - name: Generate APP_TOKEN
        uses: actions/create-github-app-token@v2
        id: app-token
        with:
          app-id: ${{ secrets.HOMEBREW_TAP_APP_ID }}
          private-key: ${{ secrets.HOMEBREW_TAP_PRIVATE_KEY }}
          repositories: homebrew-tap
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: ${{ needs.read-msrv.outputs.msrv }}
      - uses: mlugg/setup-zig@v2
      - uses: actions/setup-go@v6
        with:
          go-version: stable

      - name: Log in to Docker Hub
        uses: docker/login-action@v4
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Log in to GHCR
        uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - uses: docker/setup-qemu-action@v4
      - uses: docker/setup-buildx-action@v4

      - name: Download macOS SDK
        run: |
          curl -sL https://github.com/joseluisq/macosx-sdks/releases/download/26.1/MacOSX26.1.sdk.tar.xz -o sdk.tar.xz
          tar xf sdk.tar.xz
          echo "SDKROOT=$(pwd)/MacOSX26.1.sdk" >> $GITHUB_ENV

      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v6
        with:
          version: '~> v2'
          args: release --clean
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TAP_APP_TOKEN: ${{ steps.app-token.outputs.token }}

      - name: Prepare Docker build context
        run: |
          cp dist/idt_x86_64-unknown-linux-musl/idt idt-amd64
          cp dist/idt_aarch64-unknown-linux-musl/idt idt-arm64
      - name: Build and push Docker images
        uses: docker/build-push-action@v7
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: |
            docker.io/seonghyeon/idt:${{ github.ref_name }}
            docker.io/seonghyeon/idt:latest
            ghcr.io/sh-cho/idt:${{ github.ref_name }}
            ghcr.io/sh-cho/idt:latest

  publish:
    needs: read-msrv
    runs-on: ubuntu-latest
    environment: release
    permissions:
      id-token: write
    steps:
      - uses: actions/checkout@v6
      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: ${{ needs.read-msrv.outputs.msrv }}
      - uses: rust-lang/crates-io-auth-action@v1
        id: auth

      - name: Publish to crates.io
        run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
