name: Validate Helm Chart

on:
  pull_request:
    branches:
      - master
    paths:
      - "charts/**"
      - ".github/workflows/arc-validate-chart.yaml"
      - "!charts/actions-runner-controller/docs/**"
      - "!**.md"
      - "!charts/gha-runner-scale-set-controller/**"
      - "!charts/gha-runner-scale-set/**"
  push:
    paths:
      - "charts/**"
      - ".github/workflows/arc-validate-chart.yaml"
      - "!charts/actions-runner-controller/docs/**"
      - "!**.md"
      - "!charts/gha-runner-scale-set-controller/**"
      - "!charts/gha-runner-scale-set/**"
  workflow_dispatch:
env:
  KUBE_SCORE_VERSION: 1.10.0
  HELM_VERSION: v3.8.0

permissions:
  contents: read

concurrency:
  # This will make sure we only apply the concurrency limits on pull requests
  # but not pushes to master branch by making the concurrency group name unique
  # for pushes
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  validate-chart:
    name: Lint Chart
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Set up Helm
        uses: azure/setup-helm@1a275c3b69536ee54be43f2070a358922e12c8d4
        with:
          version: ${{ env.HELM_VERSION }}

      # python is a requirement for the chart-testing action below (supports yamllint among other tests)
      - uses: actions/setup-python@v6
        with:
          python-version: "3.11"

      - name: Set up chart-testing
        uses: helm/chart-testing-action@6ec842c01de15ebb84c8627d2744a0c2f2755c9f

      - name: Run chart-testing (list-changed)
        id: list-changed
        run: |
          changed=$(ct list-changed --config charts/.ci/ct-config.yaml)
          if [[ -n "$changed" ]]; then
            echo "changed=true" >> $GITHUB_OUTPUT
          fi

      - name: Run chart-testing (lint)
        run: |
          ct lint --config charts/.ci/ct-config.yaml

      - name: Create kind cluster
        uses: helm/kind-action@ef37e7f390d99f746eb8b610417061a60e82a6cc
        if: steps.list-changed.outputs.changed == 'true'

      # We need cert-manager already installed in the cluster because we assume the CRDs exist
      - name: Install cert-manager
        if: steps.list-changed.outputs.changed == 'true'
        run: |
          helm repo add jetstack https://charts.jetstack.io --force-update
          helm install cert-manager jetstack/cert-manager --set installCRDs=true --wait

      - name: Run chart-testing (install)
        if: steps.list-changed.outputs.changed == 'true'
        run: |
          ct install --config charts/.ci/ct-config.yaml
