# Publish chart to the GitHub Container Registry (GHCR) on push to master
# Run the following tests on PRs:
# - Check if chart's documentation is up to date
# - Check chart linting
# - Check chart installation in a Kind cluster
# - Check chart packaging

name: Chart

on:
  pull_request:
    branches: [master]
    paths: ['deploy/helm/**']
  push:
    branches: [master]
    paths: ['deploy/helm/**']

jobs:
  lint-test:
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'pull_request' }}
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 0

      - name: Check docs
        run: |
          make helm-docs
          if ! git diff --exit-code; then
            echo "error::Documentation is not up to date. Please run helm-docs and commit changes."
            exit 1
          fi

      # ct lint requires Python 3.x to run following packages:
      #  - yamale (https://github.com/23andMe/Yamale)
      #  - yamllint (https://github.com/adrienverge/yamllint)
      - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
        with:
          python-version: '3.x'

      - uses: helm/chart-testing-action@6ec842c01de15ebb84c8627d2744a0c2f2755c9f # v2.8.0

      - name: Run chart-testing (lint)
        run: ct lint --config ct.yaml

      - uses: nolar/setup-k3d-k3s@8bf8d22160e8b1d184dcb780e390d6952a7eec65 # v1.0.10
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}

      - name: Run chart-testing (install)
        run: ct install --config ct.yaml

      - name: Check chart packaging
        run: helm package deploy/helm

  publish:
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'push' }}
    permissions:
      packages: write
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - uses: docker/login-action@b45d80f862d83dbcd57f89517bcf500b2ab88fb2 # v4.0.0
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Package helm chart
        run: helm package deploy/helm

      - name: Push chart to GHCR
        run: helm push wg-portal-*.tgz oci://ghcr.io/${{ github.repository_owner }}/charts
