# build binaries and add to release - see
# https://github.com/goreleaser/goreleaser-action

name: goreleaser

on:
  workflow_dispatch:

jobs:
  release:
    permissions:
      # Create a draft GitHub release and attach the built artifacts.
      contents: write
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          # Needed otherwise we get a shallow checkout by default.
          # We need more so that goreleaser can find the previous version.
          fetch-depth: 0
      - name: Set up Go
        uses: actions/setup-go@v6
        with:
          go-version: "1.24"
      - name: Extract version
        id: extract_version
        run: |
          CMD_VERSION="$(go run ./cmd/jsonnet --version | grep -oe 'v[0-9][0-9a-z.-]*$')" &&
          printf "Extracted jsonnet version: %s\n" "${CMD_VERSION}" &&
          git tag "${CMD_VERSION}" &&
          printf "jsonnet_version='%s'\n" "${CMD_VERSION}" >> "${GITHUB_OUTPUT}"
      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@ec59f474b9834571250b370d4735c50f8e2d1e29 # v7.0.0
        env:
          FROM_COMMITISH: "${{ github.ref_name }}"
          GITHUB_TOKEN: "${{ github.token }}"
        with:
          version: latest
          args: release --clean --draft

  create_source_archive:
    needs:
      # Need to wait for goreleaser to create the draft release otherwise we end up with _two_ draft releases!
      - release
    permissions:
      contents: write
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout
        uses: actions/checkout@v6
      - name: Create release archive
        id: create_archive
        run: .github/workflows/create_archive.sh
      - name: Release
        uses: softprops/action-gh-release@a06a81a03ee405af7f2048a818ed3f03bbf83c7b # v2.5.0
        with:
          name: ${{ steps.create_archive.outputs.go_jsonnet_version }}
          tag_name: ${{ steps.create_archive.outputs.go_jsonnet_version }}
          prerelease: ${{ inputs.prerelease }}
          draft: true
          fail_on_unmatched_files: true
          files: go-jsonnet-*.tar.gz
