name: Release Build

on:
  push:
    tags:
      - "*"

jobs:
  populate-darwin-sysroot:
    runs-on: macos-latest
    steps:
      - name: Check out code
        uses: actions/checkout@v4
      - name: Populate Darwin sysroot
        run: bash .github/workflows/populate_darwin_sysroot.sh
      - name: Create Darwin sysroot tarball
        run: tar -czvf .sysroot/darwin.tar.gz -C .sysroot darwin
      - name: Upload Darwin sysroot tarball
        uses: actions/upload-artifact@v4
        with:
          name: darwin-sysroot-tarball
          path: .sysroot/darwin.tar.gz
          compression-level: 0
  build:
    runs-on: ubuntu-latest
    needs: populate-darwin-sysroot
    steps:
      - name: Check out code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: 1.20.x
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      - name: Download Darwin sysroot tarball
        uses: actions/download-artifact@v4
        with:
          name: darwin-sysroot-tarball
          path: .sysroot
      - name: Populate Darwin sysroot
        run: tar -xzvf .sysroot/darwin.tar.gz -C .sysroot
      - name: Populate Linux sysroot
        run: bash .github/workflows/populate_linux_sysroot.sh
      - name: Run GoReleaser
        env:
          GITHUB_TOKEN: ${{github.token}}
        run: |
          docker run \
            --rm \
            -e GITHUB_TOKEN=${GITHUB_TOKEN} \
            -v /var/run/docker.sock:/var/run/docker.sock \
            -v $(pwd):/go/src/llgo \
            -w /go/src/llgo \
            ghcr.io/goreleaser/goreleaser-cross:v1.22 \
            release --clean --skip nfpm,snapcraft
