name: release-oci-charts

on:
  push:
    branches:
      - main
    paths:
      - "oci/charts/apps/app-service/**"
      - "oci/charts/postgresql/**"
      - "oci/charts/observability/**"
      - ".github/workflows/release-oci-charts.yml"
  workflow_dispatch:

permissions:
  contents: read
  packages: write

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5

      - name: Setup Helm
        uses: azure/setup-helm@1a275c3b69536ee54be43f2070a358922e12c8d4
        with:
          version: v3.15.4

      - name: Log In To GHCR
        uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Lint charts
        run: |
          set -euo pipefail
          charts=(
            "oci/charts/apps/app-service"
            "oci/charts/postgresql"
            "oci/charts/observability"
          )
          for chart in "${charts[@]}"; do
            helm lint "./${chart}"
          done

      - name: Package charts
        run: |
          set -euo pipefail
          mkdir -p dist
          charts=(
            "oci/charts/apps/app-service"
            "oci/charts/postgresql"
            "oci/charts/observability"
          )
          for chart in "${charts[@]}"; do
            helm package "./${chart}" --destination dist
          done

      - name: Push charts to GHCR
        run: |
          set -euo pipefail
          for pkg in dist/*.tgz; do
            helm push "${pkg}" oci://ghcr.io/x-evor
          done
