name: 🧭 Helm Chart Release

on:
  push:
    tags:
      - "helm-v*"
  workflow_call:
    inputs:
      chart_version:
        description: "Chart version to release"
        required: true
        type: string
  workflow_dispatch:
    inputs:
      chart_version:
        description: "Chart version to release"
        required: true
        type: string

env:
  REGISTRY: ghcr.io
  CHART_NAME: trigger

jobs:
  lint-and-test:
    runs-on: warp-ubuntu-latest-x64-2x
    permissions:
      contents: read
    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          persist-credentials: false

      - name: Set up Helm
        uses: azure/setup-helm@9bc31f4ebc9c6b171d7bfbaa5d006ae7abdb4310 # v5.0.1
        with:
          version: "3.18.3"

      - name: Build dependencies
        run: helm dependency build ./hosting/k8s/helm/

      - name: Extract dependency charts
        run: |
          cd ./hosting/k8s/helm/
          for file in ./charts/*.tgz; do echo "Extracting $file"; tar -xzf "$file" -C ./charts; done

      - name: Lint Helm Chart
        run: |
          helm lint ./hosting/k8s/helm/

      - name: Render templates
        run: |
          helm template test-release ./hosting/k8s/helm/ \
            --values ./hosting/k8s/helm/values.yaml \
            --output-dir ./helm-output

      - name: Validate manifests
        uses: docker://ghcr.io/yannh/kubeconform:v0.7.0@sha256:85dbef6b4b312b99133decc9c6fc9495e9fc5f92293d4ff3b7e1b30f5611823c
        with:
          entrypoint: "/kubeconform"
          args: "-summary -output json ./helm-output"

  release:
    needs: lint-and-test
    # Set the ENABLE_HELM_PRERELEASE repository variable to 'false' to turn off
    # publishing the chart to GHCR — e.g. forks/mirrors that lack write_package
    # on the owner's charts namespace. Defaults to enabled; the lint-and-test
    # job above always runs regardless.
    if: ${{ vars.ENABLE_HELM_PRERELEASE != 'false' }}
    runs-on: warp-ubuntu-latest-x64-2x
    permissions:
      contents: write # for gh-release
      packages: write
    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          persist-credentials: false

      - name: Set up Helm
        uses: azure/setup-helm@9bc31f4ebc9c6b171d7bfbaa5d006ae7abdb4310 # v5.0.1
        with:
          version: "3.18.3"

      - name: Build dependencies
        run: helm dependency build ./hosting/k8s/helm/

      - name: Extract dependency charts
        run: |
          cd ./hosting/k8s/helm/
          for file in ./charts/*.tgz; do echo "Extracting $file"; tar -xzf "$file" -C ./charts; done

      - name: Log in to Container Registry
        uses: docker/login-action@650006c6eb7dba73a995cc03b0b2d7f5ca915bee # v4.2.0
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract version from tag or input
        id: version
        run: |
          if [ -n "${INPUTS_CHART_VERSION}" ]; then
            VERSION="${INPUTS_CHART_VERSION}"
          else
            VERSION="${GITHUB_REF_NAME}"
            VERSION="${VERSION#helm-v}"
          fi
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"
          echo "Releasing version: $VERSION"
        env:
          INPUTS_CHART_VERSION: ${{ inputs.chart_version }}

      - name: Check Chart.yaml version matches release version
        run: |
          VERSION="${STEPS_VERSION_OUTPUTS_VERSION}"
          CHART_VERSION=$(grep '^version:' ./hosting/k8s/helm/Chart.yaml | awk '{print $2}')
          echo "Chart.yaml version: $CHART_VERSION"
          echo "Release version: $VERSION"
          if [ "$CHART_VERSION" != "$VERSION" ]; then
            echo "❌ Chart.yaml version does not match release version!"
            exit 1
          fi
          echo "✅ Chart.yaml version matches release version."
        env:
          STEPS_VERSION_OUTPUTS_VERSION: ${{ steps.version.outputs.version }}

      - name: Package Helm Chart
        run: |
          helm package ./hosting/k8s/helm/ --destination /tmp/

      - name: Push Helm Chart to GHCR
        run: |
          VERSION="${STEPS_VERSION_OUTPUTS_VERSION}"
          CHART_PACKAGE="/tmp/${{ env.CHART_NAME }}-${VERSION}.tgz"

          # Push to GHCR OCI registry
          helm push "$CHART_PACKAGE" "oci://${{ env.REGISTRY }}/${{ github.repository_owner }}/charts"
        env:
          STEPS_VERSION_OUTPUTS_VERSION: ${{ steps.version.outputs.version }}

      - name: Create GitHub Release
        id: release
        uses: softprops/action-gh-release@718ea10b132b3b2eba29c1007bb80653f286566b # v3.0.1
        with:
          tag_name: helm-v${{ steps.version.outputs.version }}
          name: "Helm Chart ${{ steps.version.outputs.version }}"
          body: |
            ### Installation
            ```bash
            helm upgrade --install trigger \
              oci://${{ env.REGISTRY }}/${{ github.repository_owner }}/charts/${{ env.CHART_NAME }} \
              --version "${{ steps.version.outputs.version }}"
            ```

            ### Changes
            See commit history for detailed changes in this release.
          files: |
            /tmp/${{ env.CHART_NAME }}-${{ steps.version.outputs.version }}.tgz
          token: ${{ secrets.GITHUB_TOKEN }}
          draft: true
          prerelease: true
