name: Go Build

on:
  push:
    branches:
      - main
  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Install Go
        id: install-go
        uses: actions/setup-go@v6
        with:
          go-version-file: 'go.mod'
          cache: false

      - name: Cache Go mod
        id: gomod
        uses: actions/cache@v5
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-mod-

      - name: Cache Go build
        uses: actions/cache@v5
        with:
          path: ~/.cache/go-build
          key: ${{ runner.os }}-go-build-${{ github.ref_name }}
          restore-keys: |
            ${{ runner.os }}-go-build-

      - name: Download dependencies
        run: go mod download
        if: steps.gomod.outputs.cache-hit != 'true'

      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v7
        env:
          GITHUB_TOKEN: ${{ secrets.GORELEASER_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
        with:
          distribution: goreleaser
          version: ${{ inputs.GORELEASER_VERSION }}
          args: release --clean --snapshot --skip=docker
