name: Builder - Snapshot build
on:
  push:
    branches: [main]

  # on PRs touching the builder
  pull_request:
    branches: [main]
    paths:
      - "cmd/builder/**"

permissions:
  contents: read

env:
  # renovate: datasource=github-tags depName=goreleaser-pro packageName=goreleaser/goreleaser-pro
  GORELEASER_PRO_VERSION: v2.11.1

jobs:
  snapshot:
    runs-on: ubuntu-24.04
    if: ${{ github.repository_owner == 'open-telemetry' }}
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          path: .core

      - name: Pull the latest releases repo
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          path: opentelemetry-collector-releases
          repository: open-telemetry/opentelemetry-collector-releases

      - name: Copy release files
        run: cp -R ./opentelemetry-collector-releases/cmd/builder/. ./.core/cmd/builder/

      - uses: sigstore/cosign-installer@cad07c2e89fa2edd6e2d7bab4c1aa38e53f76003 # v4.1.1

      - uses: anchore/sbom-action/download-syft@e22c389904149dbc22b58101806040fa8d37a610 # v0.24.0

      - uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0
        with:
          platforms: amd64,arm64,ppc64le,s390x,riscv64

      - uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0

      - name: Setup Go
        uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
        with:
          go-version: stable
          cache: false

      - name: Cache Go
        id: go-cache
        uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
        with:
          path: |
            ~/go/bin
            ~/go/pkg/mod
          key: go-cache-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('**/go.sum') }}

      - name: Check GoReleaser
        uses: goreleaser/goreleaser-action@1a80836c5c9d9e5755a25cb59ec6f45a3b5f41a8 # v7.2.1
        with:
          distribution: goreleaser-pro
          version: ${{ env.GORELEASER_PRO_VERSION }}
          args: check --verbose -f .core/cmd/builder/.goreleaser.yaml
        env:
          GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@1a80836c5c9d9e5755a25cb59ec6f45a3b5f41a8 # v7.2.1
        with:
          distribution: goreleaser-pro
          version: ${{ env.GORELEASER_PRO_VERSION }}
          args: --snapshot --clean -f .core/cmd/builder/.goreleaser.yaml --skip sign
        env:
          GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          COSIGN_YES: false

        # Only create an issue if the workflows fails on push to main branch
      - name: File an issue if the workflow failed
        if: failure() && github.ref == 'refs/heads/main'
        run: |
          template=$(cat <<'END'
          [Link to job log](%s)
          END
          )
          job_url="$(gh run view ${{ github.run_id }} -R ${{ github.repository }} --json jobs -q '.jobs[] | select(.name == "snapshot") | .url')"
          body="$(printf "$template" "$job_url")"
          gh issue create -R ${{ github.repository }} -t 'OCB snapshot workflow failed' -b "$body" -l 'ci-cd' -l 'area:builder'
        env:
          GH_TOKEN: ${{ github.token }}
