name: "Mgbench Bolt Client Publish Docker Image"

on:
  workflow_dispatch:
    inputs:
      version:
        description: "Mgbench bolt client version to publish on Dockerhub."
        required: true
      force_release:
        type: boolean
        required: false
        default: false

jobs:
  mgbench_docker_publish:
    runs-on: ubuntu-latest
    env:
      DOCKER_ORGANIZATION_NAME: memgraph
      DOCKER_REPOSITORY_NAME: mgbench-client

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

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

      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Check if specified version is already pushed
        run: |
          EXISTS=$(docker manifest inspect $DOCKER_ORGANIZATION_NAME/$DOCKER_REPOSITORY_NAME:${{ github.event.inputs.version }} > /dev/null; echo $?)
          echo $EXISTS
          if [[ ${EXISTS} -eq 0 ]]; then
              echo 'The specified version has been already released to DockerHub.'
              if [[ ${{ github.event.inputs.force_release }} = true ]]; then
                echo 'Forcing the release!'
              else
                echo 'Stopping the release!'
                exit 1
              fi
          else
              echo 'All good the specified version has not been release to DockerHub.'
          fi

      - name: Build & push docker images
        run: |
          cd tests/mgbench
          docker buildx build \
          --build-arg TOOLCHAIN_VERSION=toolchain-v4 \
          --platform linux/amd64,linux/arm64 \
          --tag $DOCKER_ORGANIZATION_NAME/$DOCKER_REPOSITORY_NAME:${{ github.event.inputs.version }} \
          --tag $DOCKER_ORGANIZATION_NAME/$DOCKER_REPOSITORY_NAME:latest \
          --file Dockerfile.mgbench_client \
          --push .
