name: Publish charts
permissions: read-all
on:
  push:
    tags:
      - 'v*'

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  publish-helm:
    # Skip this Release on forks
    if: github.repository_owner == 'projectcapsule'
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
      - name: "Extract Version"
        id: extract_version
        run: |
          GIT_TAG=${GITHUB_REF##*/}
          VERSION=${GIT_TAG##*v}
          echo "version=$(echo $VERSION)" >> $GITHUB_OUTPUT
      - name: Publish Helm chart
        uses: stefanprodan/helm-gh-pages@0ad2bb377311d61ac04ad9eb6f252fb68e207260 # v1.7.0
        with:
          token: "${{ secrets.HELM_CHARTS_PUSH_TOKEN }}"
          linting: off
          chart_version: ${{ steps.extract_version.outputs.version }}
          app_version: ${{ steps.extract_version.outputs.version }}
          charts_dir: charts
          charts_url: https://${{ github.repository_owner }}.github.io/charts
          owner: ${{ github.repository_owner }}
          repository: charts
          branch: gh-pages
          commit_username: ${{ github.actor }}
  publish-helm-oci:
    runs-on: ubuntu-20.04
    permissions:
      contents: write
      id-token: write
      packages: write
    outputs:
      chart-digest: ${{ steps.helm_publish.outputs.digest }}
    steps:
      - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
      - uses: sigstore/cosign-installer@e1523de7571e31dbe865fd2e80c5c7c23ae71eb4 # v3.4.0
      - name: "Extract Version"
        id: extract_version
        run: |
          GIT_TAG=${GITHUB_REF##*/}
          VERSION=${GIT_TAG##*v}
          echo "version=$(echo $VERSION)" >> $GITHUB_OUTPUT
      - name: Helm | Publish
        id: helm_publish
        uses: peak-scale/github-actions/helm-oci-chart@38322faabccd75abfa581c435e367d446b6d2c3b # v0.1.0
        with:
          registry: ghcr.io
          repository: ${{ github.repository_owner }}/charts
          name: "capsule"
          version: ${{ steps.extract_version.outputs.version }}
          app-version: ${{ steps.extract_version.outputs.version }}
          registry-username: ${{ github.actor }}
          registry-password: ${{ secrets.GITHUB_TOKEN }}
          update-dependencies: 'true' # Defaults to false
          sign-image: 'true'
          signature-repository: ghcr.io/${{ github.repository_owner }}/signatures
  helm-provenance:
    needs: publish-helm-oci
    permissions:
      id-token: write   # To sign the provenance.
      packages: write   # To upload assets to release.
      actions: read     # To read the workflow path.
    uses: slsa-framework/slsa-github-generator/.github/workflows/generator_container_slsa3.yml@v1.9.0
    with:
      image: ghcr.io/${{ github.repository_owner }}/charts/capsule
      digest: "${{ needs.publish-helm-oci.outputs.chart-digest }}"
      registry-username: ${{ github.actor }}
    secrets:
      registry-password: ${{ secrets.GITHUB_TOKEN }}
