name: Release
on:
  release:
    types: [published]

jobs:
  release:
    runs-on: ubuntu-latest
    outputs:
      version: ${{steps.build.outputs.version}}
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - run: |
          git config user.name github-actions
          git config user.email github-actions@github.com

      - name: Set up JDK
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'zulu'
          cache: 'maven'

      - name: Build with Maven
        id: build
        run: |
          ./mvnw -B -ntp versions:set -DnewVersion=${{ github.event.release.tag_name }}
          ./mvnw -B -V -ntp clean package -Pprod -DskipTests
          export VERSION=$(./mvnw -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec)
          echo "version=${VERSION}" >> $GITHUB_OUTPUT

      - name: Upload files to a GitHub release
        uses: svenstaro/upload-release-action@2.3.0
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: kafka-ui-api/target/kafka-ui-api-${{ steps.build.outputs.version }}.jar
          tag: ${{ github.event.release.tag_name }}

      - name: Archive JAR
        uses: actions/upload-artifact@v3
        with:
          name: kafka-ui-${{ steps.build.outputs.version }}
          path: kafka-ui-api/target/kafka-ui-api-${{ steps.build.outputs.version }}.jar
#################
#               #
# Docker images #
#               #
#################
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Cache Docker layers
        uses: actions/cache@v3
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-buildx-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-buildx-

      - name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and push
        id: docker_build_and_push
        uses: docker/build-push-action@v3
        with:
          builder: ${{ steps.buildx.outputs.name }}
          context: kafka-ui-api
          platforms: linux/amd64,linux/arm64
          push: true
          tags: |
            provectuslabs/kafka-ui:${{ steps.build.outputs.version }}
            provectuslabs/kafka-ui:latest
          build-args: |
            JAR_FILE=kafka-ui-api-${{ steps.build.outputs.version }}.jar
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache

  charts:
    runs-on: ubuntu-latest
    needs: release
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 1

      - run: |
          git config user.name github-actions
          git config user.email github-actions@github.com

      - uses: azure/setup-helm@v1

      - name: update chart version
        run: |
          export version=${{needs.release.outputs.version}}
          sed -i "s/version:.*/version: ${version}/" charts/kafka-ui/Chart.yaml
          sed -i "s/appVersion:.*/appVersion: ${version}/" charts/kafka-ui/Chart.yaml

      - name: add chart
        run: |
          export VERSION=${{needs.release.outputs.version}}
          MSG=$(helm package --app-version ${VERSION} charts/kafka-ui)
          git fetch origin
          git stash
          git checkout -b gh-pages origin/gh-pages
          helm repo index .
          git add -f ${MSG##*/} index.yaml
          git commit -m "release ${VERSION}"
          git push
