name: Release
on:
  push:
    tags:
      - "v*"

permissions: read-all

jobs:
  release:
    runs-on: ubuntu-20.04
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version: 1.18
      - name: Login to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USER }}
          password: ${{ secrets.DOCKER_PASS }}
      - name: Login to AWS Public ECR
        uses: docker/login-action@v2
        with:
          registry: public.ecr.aws
          username: ${{ secrets.AWS_ACCESS_KEY_ID }}
          password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      - name: Release Docker images and CLI tool binaries
        uses: goreleaser/goreleaser-action@v2
        with:
          version: latest
          args: release --rm-dist --timeout 60m
        env:
          GITHUB_TOKEN: ${{ secrets.GHT_RELEASE }}
      - name: Set up Helm
        uses: azure/setup-helm@v2.1
      - name: Checkout Helm charts repo
        uses: actions/checkout@v3
        with:
          repository: artifacthub/helm-charts
          path: helm-charts
          token: ${{ secrets.GHT_RELEASE }}
      - name: Extract tag name
        id: extract_tag_name
        run: echo ::set-output name=tag::${GITHUB_REF#refs/tags/}
      - name: Release Helm chart
        run: |
          mkdir tmp && cd tmp
          helm package -u ../charts/artifact-hub
          helm repo index --merge ../helm-charts/index.yaml --url https://artifacthub.github.io/helm-charts .
          mv * ../helm-charts
          cd ../helm-charts
          git config user.name artifacthubio
          git config user.email hub@artifacthub.io
          git add .
          git commit -m "Add Artifact Hub chart package ${{steps.extract_tag_name.outputs.tag}}"
          git push
