name: 'VIB Publish'
on: # rebuild any PRs and main branch changes
  push:
    branches:
      - master
    paths:
      - 'bitnami/wordpress/Chart.yaml'
      - 'bitnami/grafana-loki/Chart.yaml'
      - 'bitnami/haproxy-intel/Chart.yaml'
      - 'bitnami/sealed-secrets/Chart.yaml'
env:
  CSP_API_URL: https://console.cloud.vmware.com
  CSP_API_TOKEN: ${{ secrets.CSP_API_TOKEN }}
  VIB_PUBLIC_URL: https://cp.bromelia.vmware.com
jobs:
  get-chart:
    runs-on: ubuntu-latest
    name: 'Get modified charts'
    outputs:
      chart: ${{ steps.get-chart.outputs.chart }}
      result: ${{ steps.get-chart.outputs.result }}
    steps:
      - uses: actions/checkout@v2
        with:
          path: charts
          fetch-depth: 2 # to be able to obtain files changed in the latest commit
      - id: get-chart
        name: 'Get modified charts'
        run: |
          cd charts
          files_changed="$(git show --pretty="" --name-only)"
          # Adding || true to avoid "Process exited with code 1" errors
          charts_dirs_changed="$(echo "$files_changed" | xargs dirname | grep -o "bitnami/[^/]*" | sort | uniq || true)"
          # Using grep -c as a better alternative to wc -l when dealing with empty strings."
          num_charts_changed="$(echo "$charts_dirs_changed" | grep -c "bitnami" || true)"
          num_version_bumps="$(echo "$files_changed" | grep Chart.yaml | xargs git show | grep -c "+version" || true)"

          if [[ "$num_charts_changed" -ne "$num_version_bumps" ]]; then
            # Changes done in charts but version not bumped -> ERROR
            echo "::set-output name=error::Detected changes in charts without version bump in Chart.yaml.\nCharts changed: ${num_charts_changed}\n${charts_dirs_changed}\nVersion bumps detected: ${num_version_bumps}"
            echo "::set-output name=result::fail"
          elif [[ "$num_charts_changed" -eq "1" ]]; then
            # Changes done in only one chart -> OK
            chart_name=$(echo "$charts_dirs_changed" | sed "s|bitnami/||g")
            echo "::set-output name=chart::${chart_name}"
            echo "::set-output name=result::ok"
          else
            # Changes done in more than chart -> FAIL
            echo -e "::set-output name=error::Changes detected in more than one chart directory:\n${charts_dirs_changed}\nThe publish process will be stopped. Please create different commits for each chart."
            echo "::set-output name=result::fail"
          fi
      - id: show-error
        name: 'Show error'
        if: ${{ steps.get-chart.outputs.result == 'fail' }}
        uses: actions/github-script@v3
        with:
          script: |
              core.setFailed('${{ steps.get-chart.outputs.error }}')

  vib-publish:
    runs-on: ubuntu-latest
    needs: get-chart
    if: ${{ needs.get-chart.outputs.result == 'ok' }}
    name: Publish
    steps:
      - uses: actions/checkout@v2
        name: Checkout Repository
        with:
          path: charts
      - uses: vmware-labs/vmware-image-builder-action@main
        name: Verify and publish ${{ needs.get-chart.outputs.chart }}
        with:
          pipeline: ${{ needs.get-chart.outputs.chart }}/vib-publish.json
          config: charts/.vib/
        env:
          VIB_ENV_TARGET_PLATFORM: 7ddab896-2e4e-4d58-a501-f79897eba3a0
          VIB_ENV_ALTERNATIVE_TARGET_PLATFORM: 91d398a2-25c4-4cda-8732-75a3cfc179a1
          VIB_ENV_S3_URL: s3://${{ secrets.AWS_S3_BUCKET }}/bitnami
          VIB_ENV_S3_USERNAME: ${{ secrets.AWS_ACCESS_KEY_ID }}
          VIB_ENV_S3_PASSWORD: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      # If we perform a checkout of the master branch, we will find conflicts with the submodules
      - uses: actions/checkout@v2
        with:
          ref: 'index'
          path: index
          token: ${{ secrets.BITNAMI_BOT_TOKEN }}
      - name: Install and configure aws-cli, helm and yq
        run: |
          # AWS CLI
          sudo DEBIAN_FRONTEND=noninteractive apt-get install -y awscli
          aws configure set region us-east-1
          aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws configure set source_profile default
            # helm
          HELM_TARBALL="helm-v3.8.1-linux-amd64.tar.gz"
          curl -SsLfO "https://get.helm.sh/${HELM_TARBALL}" && sudo tar xf "$HELM_TARBALL" --strip-components 1 -C /usr/local/bin
          # yq
          sudo curl -SsLf https://github.com/mikefarah/yq/releases/download/v4.23.1/yq_linux_amd64 -o /usr/local/bin/yq && sudo chmod +x /usr/local/bin/yq
      - name: Fetch chart and update index
        run: |
          mkdir download
          chart="${{ needs.get-chart.outputs.chart }}"
          # HACK: Obtain chart tarball out of the latest commit
          chart_version="$(yq ".version" charts/bitnami/${chart}/Chart.yaml)"
          aws s3 cp s3://${{ secrets.AWS_S3_BUCKET }}/bitnami/${chart}-${chart_version}.tgz download/
          # Rebuild index
          helm repo index --url https://charts.bitnami.com/bitnami --merge index/bitnami/index.yaml download
          cp download/index.yaml index/bitnami/index.yaml
          cd index
          # Push changes
          git config user.name "Bitnami Containers"
          git config user.email containers@bitnami.com
          git add bitnami/index.yaml && git commit -m "${chart}-${chart_version}: Update index.yaml" -s && git push
