name: DockerHub Build and Push

#`DOCKER_HUB_USERNAME` is the username you use to log in to Docker Hub at https://hub.docker.com/. It's your Docker Hub
# account username.

#`DOCKER_HUB_ACCESS_TOKEN` is a security token that you should create in your Docker Hub account settings, specifically
# under "account settings / security." This token should be generated with read and write access permissions to Docker
# Hub repositories. It allows you to authenticate and interact with Docker Hub programmatically, such as pushing and pulling Docker images or making API requests.

on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'               # Trigger for tags like v1.2.3
      - '[0-9]+.[0-9]+.[0-9]+'                # Trigger for tags like 1.2.3 (without 'v')

env:
  CARGO_TERM_COLOR: always
  LATEST_TAG: latest

jobs:
  appflowy_cloud_image:
    runs-on: ubuntu-22.04
    env:
      IMAGE_NAME: ${{ secrets.DOCKER_HUB_USERNAME }}/appflowy_cloud
    strategy:
      fail-fast: false
      matrix:
        job:
          - { name: "amd64",   docker_platform: "linux/amd64" }
          - { name: "arm64v8", docker_platform: "linux/arm64" }

    steps:
      - name: Check out the repository
        uses: actions/checkout@v2

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

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

      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_HUB_USERNAME }}
          password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

      - name: Get git tag
        id: vars
        run: |
          T=${GITHUB_REF#refs/*/}   # Remove "refs/*/" prefix from GITHUB_REF
          echo "GIT_TAG=$T" >> $GITHUB_ENV 

      - name: Extract metadata
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: registry.hub.docker.com/${{ env.IMAGE_NAME }}

      - name: Build and push ${{ matrix.job.image_name }}:${{ env.GIT_TAG }}
        uses: docker/build-push-action@v5
        with:
          platforms: ${{ matrix.job.docker_platform }}
          push: true
          tags: |
            ${{ env.IMAGE_NAME }}:${{ env.LATEST_TAG }}-${{ matrix.job.name }}
            ${{ env.IMAGE_NAME }}:${{ env.GIT_TAG }}-${{ matrix.job.name }}
          labels: ${{ steps.meta.outputs.labels }}
          provenance: false

      - name: Logout from Docker Hub
        if: always()
        run: docker logout

  appflowy_cloud_docker_manifest:
    runs-on: ubuntu-22.04
    needs: [ appflowy_cloud_image ]
    strategy:
      fail-fast: false
      matrix:
        job:
          - { image_name: "appflowy_cloud" }

    steps:
      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_HUB_USERNAME }}
          password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

      - name: Get git tag
        id: vars
        run: |
          T=${GITHUB_REF#refs/*/}   # Remove "refs/*/" prefix from GITHUB_REF
          echo "GIT_TAG=$T" >> $GITHUB_ENV 

      - name: Create and push manifest for ${{ matrix.job.image_name }}:version
        uses: Noelware/docker-manifest-action@master
        with:
          inputs: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ matrix.job.image_name }}:${{ env.GIT_TAG }}
          images: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ matrix.job.image_name }}:${{ env.GIT_TAG }}-amd64,${{ secrets.DOCKER_HUB_USERNAME }}/${{ matrix.job.image_name }}:${{ env.GIT_TAG }}-arm64v8
          push: true

      - name: Create and push manifest for ${{ matrix.job.image_name }}:latest
        uses: Noelware/docker-manifest-action@master
        with:
          inputs: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ matrix.job.image_name }}:${{ env.LATEST_TAG }}
          images: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ matrix.job.image_name }}:${{ env.LATEST_TAG }}-amd64,${{ secrets.DOCKER_HUB_USERNAME }}/${{ matrix.job.image_name }}:${{ env.LATEST_TAG }}-arm64v8
          push: true

      - name: Logout from Docker Hub
        if: always()
        run: docker logout

  admin_frontend_image:
    runs-on: ubuntu-22.04
    env:
      IMAGE_NAME: ${{ secrets.DOCKER_HUB_USERNAME }}/admin_frontend
    strategy:
      fail-fast: false
      matrix:
        job:
          - { name: "amd64",   docker_platform: "linux/amd64" }
          - { name: "arm64v8", docker_platform: "linux/arm64" }

    steps:
      - name: Check out the repository
        uses: actions/checkout@v2

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

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

      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_HUB_USERNAME }}
          password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

      - name: Get git tag
        id: vars
        run: |
          T=${GITHUB_REF#refs/*/}   # Remove "refs/*/" prefix from GITHUB_REF
          echo "GIT_TAG=$T" >> $GITHUB_ENV 

      - name: Extract metadata
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: registry.hub.docker.com/${{ env.IMAGE_NAME }}

      - name: Build and push ${{ matrix.job.image_name }}:${{ env.GIT_TAG }}
        uses: docker/build-push-action@v5
        with:
          context: ./admin_frontend
          platforms: ${{ matrix.job.docker_platform }}
          push: true
          tags: |
            ${{ env.IMAGE_NAME }}:${{ env.LATEST_TAG }}-${{ matrix.job.name }}
            ${{ env.IMAGE_NAME }}:${{ env.GIT_TAG }}-${{ matrix.job.name }}
          labels: ${{ steps.meta.outputs.labels }}
          provenance: false

      - name: Logout from Docker Hub
        if: always()
        run: docker logout

  admin_frontend_docker_manifest:
    runs-on: ubuntu-22.04
    needs: [admin_frontend_image]
    strategy:
      fail-fast: false
      matrix:
        job:
          - { image_name: "admin_frontend" }

    steps:
      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_HUB_USERNAME }}
          password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

      - name: Get git tag
        id: vars
        run: |
          T=${GITHUB_REF#refs/*/}   # Remove "refs/*/" prefix from GITHUB_REF
          echo "GIT_TAG=$T" >> $GITHUB_ENV 

      - name: Create and push manifest for ${{ matrix.job.image_name }}:version
        uses: Noelware/docker-manifest-action@master
        with:
          inputs: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ matrix.job.image_name }}:${{ env.GIT_TAG }}
          images: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ matrix.job.image_name }}:${{ env.GIT_TAG }}-amd64,${{ secrets.DOCKER_HUB_USERNAME }}/${{ matrix.job.image_name }}:${{ env.GIT_TAG }}-arm64v8
          push: true

      - name: Create and push manifest for ${{ matrix.job.image_name }}:latest
        uses: Noelware/docker-manifest-action@master
        with:
          inputs: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ matrix.job.image_name }}:${{ env.LATEST_TAG }}
          images: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ matrix.job.image_name }}:${{ env.LATEST_TAG }}-amd64,${{ secrets.DOCKER_HUB_USERNAME }}/${{ matrix.job.image_name }}:${{ env.LATEST_TAG }}-arm64v8
          push: true

      - name: Logout from Docker Hub
        if: always()
        run: docker logout