name: build

on:
  push:
    branches:
      - main
      - release/**
  workflow_dispatch:

env:
  PKG_NAME: "terraform-provider-helm"

jobs:
  get-go-version:
    name: "Detect Go toolchain version"
    runs-on: ubuntu-latest
    outputs:
      go-version: ${{ steps.get-go-version.outputs.go-version }}
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
      - uses: actions/setup-go@f111f3307d8850f501ac008e886eec1fd1932a34 # v5.3.0
        with:
          go-version-file: 'go.mod'
      - name: Detect Go version
        id: get-go-version
        run: |
          version="$(go list -f {{.GoVersion}} -m)"
          echo "go-version=$version" >> "$GITHUB_OUTPUT"

  set-product-version:
    name: "Parse version file"
    runs-on: ubuntu-latest
    outputs:
      product-version: ${{ steps.set-product-version.outputs.product-version }}
      product-base-version: ${{ steps.set-product-version.outputs.base-product-version }}
      product-prerelease-version: ${{ steps.set-product-version.outputs.prerelease-product-version }}
      product-minor-version: ${{ steps.set-product-version.outputs.minor-product-version }}
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
      - name: Set variables
        id: set-product-version
        uses: hashicorp/actions-set-product-version@v2

  generate-metadata-file:
    needs: set-product-version
    runs-on: ubuntu-latest
    outputs:
      filepath: ${{ steps.generate-metadata-file.outputs.filepath }}
    steps:
      - name: "Checkout directory"
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
      - name: Generate metadata file
        id: generate-metadata-file
        uses: hashicorp/actions-generate-metadata@v1
        with:
          version: ${{ needs.set-product-version.outputs.product-version }}
          product: ${{ env.PKG_NAME }}
          repositoryOwner: "hashicorp"
      - uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 # v4.6.1
        with:
          name: metadata.json
          path: ${{ steps.generate-metadata-file.outputs.filepath }}

  upload-terraform-registry-manifest-artifact:
    needs: set-product-version
    runs-on: ubuntu-latest
    steps:
      - name: "Checkout directory"
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          path: ${{ env.PKG_NAME }}
      - name: "Copy manifest from checkout directory to a file with the desired name"
        id: terraform-registry-manifest
        run: |
          name="${{ env.PKG_NAME }}"
          version="${{ needs.set-product-version.outputs.product-version }}"

          source="${name}/terraform-registry-manifest.json"
          destination="${name}_${version}_manifest.json"

          cp "$source" "$destination"
          echo "filename=$destination" >> "$GITHUB_OUTPUT"
      - uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 # v4.6.1
        with:
          name: terraform-registry-manifest.json
          path: ${{ steps.terraform-registry-manifest.outputs.filename }}
          if-no-files-found: error

  build:
    needs:
      - get-go-version
      - set-product-version
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        goos: [freebsd, windows, linux, darwin]
        goarch: ["386", "amd64", "arm", "arm64"]
        exclude:
          - goos: freebsd
            goarch: arm64
          - goos: windows
            goarch: arm64
          - goos: windows
            goarch: arm
          - goos: darwin
            goarch: 386
          - goos: darwin
            goarch: arm

    name: Go ${{ needs.get-go-version.outputs.go-version }} ${{ matrix.goos }} ${{ matrix.goarch }} build
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
      - uses: hashicorp/actions-go-build@v1
        env:
          CGO_ENABLED: 0
          BASE_VERSION: ${{ needs.set-product-version.outputs.product-base-version }}
          PRERELEASE_VERSION: ${{ needs.set-product-version.outputs.product-prerelease-version}}
          METADATA_VERSION: ${{ env.METADATA }}
        with:
          bin_name: "${{ env.PKG_NAME }}_v${{ needs.set-product-version.outputs.product-version }}_x5"
          product_name: ${{ env.PKG_NAME }}
          product_version: ${{ needs.set-product-version.outputs.product-version }}
          go_version: ${{ needs.get-go-version.outputs.go-version }}
          os: ${{ matrix.goos }}
          arch: ${{ matrix.goarch }}
          reproducible: report
          instructions: |
            go build \
              -o "$BIN_PATH" \
              -trimpath \
              -buildvcs=false \
              -ldflags "-s -w -X 'main.Version=${{ needs.set-product-version.outputs.product-version }}'"
            cp LICENSE "$TARGET_DIR/LICENSE.txt"

  whats-next:
    needs:
      - build
      - generate-metadata-file
      - upload-terraform-registry-manifest-artifact
    runs-on: ubuntu-latest
    name: "What's next?"
    steps:
      - name: "Write a helpful summary"
        run: |
          github_dot_com="${{ github.server_url }}"
          owner_with_name="${{ github.repository }}"
          ref="${{ github.ref }}"

          echo "### What's next?" >> "$GITHUB_STEP_SUMMARY"
          echo "#### For a release branch (see \`.release/ci.hcl\`)" >> $GITHUB_STEP_SUMMARY
          echo "After this \`build\` workflow run completes succesfully, you can expect the CRT \`prepare\` workflow to begin momentarily." >> "$GITHUB_STEP_SUMMARY"
          echo "To find the \`prepare\` workflow run, [view the checks for this commit]($github_dot_com/$owner_with_name/commits/$ref)" >> "$GITHUB_STEP_SUMMARY"
