name: release-snapshot

on:
  push:
    branches:
      - "main"
      - "demo-*"
      - "bugbash-*"

  # Confirm that snapshot builds work if this file is modified.
  pull_request:
    types:
      - opened
      - synchronize
      - reopened
    paths:
      - ".github/workflows/release-snapshot.yml"

  workflow_dispatch:

jobs:
  goreleaser:
    runs-on:
      group: databricks-deco-testing-runner-group
      labels: ubuntu-latest-deco

    steps:
      - name: Checkout repository and submodules
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          fetch-depth: 0
          fetch-tags: true

      - name: Setup Go
        uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
        with:
          go-version: 1.23.4

          # The default cache key for this action considers only the `go.sum` file.
          # We include .goreleaser.yaml here to differentiate from the cache used by the push action
          # that runs unit tests. This job produces and uses a different cache.
          cache-dependency-path: |
            go.sum
            .goreleaser.yaml

      - name: Hide snapshot tag to outsmart GoReleaser
        run: git tag -d snapshot || true

      - name: Run GoReleaser
        id: releaser
        uses: goreleaser/goreleaser-action@9ed2f89a662bf1735a48bc8557fd212fa902bebf # v6.1.0
        with:
          version: ~> v2
          args: release --snapshot --skip docker

      - name: Upload macOS binaries
        uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
        with:
          name: cli_darwin_snapshot
          path: |
            dist/*_darwin_*/

      - name: Upload Linux binaries
        uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
        with:
          name: cli_linux_snapshot
          path: |
            dist/*_linux_*/

      - name: Upload Windows binaries
        uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
        with:
          name: cli_windows_snapshot
          path: |
            dist/*_windows_*/

      - name: Update snapshot tag

        # Snapshot release may only be updated for commits to the main branch.
        if: github.ref == 'refs/heads/main'

        run: |
          git tag snapshot
          git push origin snapshot --force

      - name: Update snapshot release

        # Snapshot release may only be updated for commits to the main branch.
        if: github.ref == 'refs/heads/main'

        uses: softprops/action-gh-release@de2c0eb89ae2a093876385947365aca7b0e5f844 # v1
        with:
          name: Snapshot
          prerelease: true
          tag_name: snapshot
          token: ${{ secrets.GITHUB_TOKEN }}
          files: |
            dist/databricks_cli_*.zip