name: Build Release

on:
  push:
    tags:
      - 'v*'

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v3

      - name: GitHub slug
        uses: rlespinasse/github-slug-action@v4
        with:
          short-length: 8

      - name: Make releases directory structure
        run: mkdir -p $GITHUB_WORKSPACE/${{ env.GITHUB_REF_SLUG }}

      - name: Install Helm
        uses: azure/setup-helm@v3
        with:
          version: 3.12.3

      - name: helm lint
        run: helm lint helm-chart-sources/*

      - name: helm package
        run: helm package helm-chart-sources/* -d ${{ env.GITHUB_REF_SLUG }}/

      - name: helm repo
        run: helm repo index --url https://github.com/criblio/helm-charts/releases/download/ --merge index.yaml .

      - name: Upload binaries to release
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: ${{ env.GITHUB_REF_SLUG }}/*.tgz
          file_glob: true
          tag: ${{ env.GITHUB_REF_SLUG }}
          overwrite: true

      - name: Setup git config
        run: |
          git config user.name "Github Release Bot"
          git config user.email "<>"

      - name: Push commit
        run: |
          git fetch
          git checkout master
          git add index.yaml
          git commit -m "Release ${{ env.GITHUB_REF_SLUG }}"
          git push origin master