name: helm-release

on:
  push:
    tags:
      - "chart-v*"

jobs:
  release:
    permissions: write-all
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
        with:
          fetch-depth: 0

      - name: configure git
        run: |
          git config user.name "$GITHUB_ACTOR"
          git config user.email "$GITHUB_ACTOR@users.noreply.github.com"

      - name: validate chart version
        id: version
        run: |
          tag_version="${GITHUB_REF#refs/tags/chart-v}"
          chart_version=$(yq '.version' charts/readur/Chart.yaml)
          if [[ "$chart_version" == "$tag_version" ]]; then
            echo "should_release=true" >> $GITHUB_OUTPUT
            echo "Chart version $chart_version matches tag chart-v$tag_version"
          else
            echo "should_release=false" >> $GITHUB_OUTPUT
            echo "Error: Chart version ($chart_version) does not match tag version ($tag_version)"
            exit 1
          fi

      - name: run chart-releaser
        id: cr
        if: ${{ steps.version.outputs.should_release == 'true' }}
        uses: helm/chart-releaser-action@cae68fefc6b5f367a0275617c9f83181ba54714f # v1.7.0
        env:
          CR_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
          CR_GENERATE_RELEASE_NOTES: true
          CR_SKIP_EXISTING: true

      - name: Login to GitHub Container Registry
        if: ${{ steps.version.outputs.should_release == 'true' }}
        uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 # v3
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - if: ${{ steps.version.outputs.should_release == 'true' }}
        name: Push Chart
        run: helm push .cr-release-packages/readur-*.tgz oci://ghcr.io/${{ github.repository }}
