name: Publish Docker Image
on: [push]

concurrency:
  group: ${{ github.ref }}-${{ github.workflow }}
  cancel-in-progress: true

jobs:
  docker_build:
    name: Build ${{ matrix.arch }} Image
    runs-on: ubuntu-latest

    strategy:
      matrix:
        include:
          - arch: amd64
            name: amd64
        #   - arch: arm64
        #     name: arm64

    steps:
      - name: Free up disk spaces
        run: |
          sudo rm -rf /usr/share/dotnet || true
          sudo rm -rf /opt/ghc || true
          sudo rm -rf "/usr/local/share/boost" || true
          sudo rm -rf "$AGENT_TOOLSDIRECTORY" || true

      - name: Get lowercase string for the repository name
        id: lowercase-repo-name
        uses: ASzc/change-string-case-action@v2
        with:
          string: ${{ github.event.repository.name }}

      - name: Checkout base
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Cache Docker layers
        uses: actions/cache@v2
        with:
          path: /tmp/.buildx-cache
          key: ${{ github.ref }}-${{ matrix.arch }}
          restore-keys: |
            ${{ github.ref }}-${{ matrix.arch }}

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2
        with:
          platforms: linux/${{ matrix.arch }}

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

      - name: Docker login
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Get commit SHA
        id: vars
        run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"

      - name: Build and export
        id: build
        if: github.ref == 'refs/heads/master'
        uses: docker/build-push-action@v3
        with:
          push: true
          platforms: linux/${{ matrix.arch }}
          tags: ${{ secrets.DOCKER_USERNAME }}/${{ steps.lowercase-repo-name.outputs.lowercase }}:${{ matrix.name }}-latest
          build-args: |
            SHA=${{ steps.vars.outputs.sha_short }}
          outputs: type=image,push=true
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache

      - name: Replace tag without `v`
        if: startsWith(github.ref, 'refs/tags/')
        uses: actions/github-script@v1
        id: version
        with:
          script: |
            return context.payload.ref.replace(/\/?refs\/tags\/v/, '')
          result-encoding: string

      - name: Build release and export
        id: build_rel
        if: startsWith(github.ref, 'refs/tags/')
        uses: docker/build-push-action@v3
        with:
          push: true
          platforms: linux/${{ matrix.arch }}
          tags: ${{ secrets.DOCKER_USERNAME }}/${{ steps.lowercase-repo-name.outputs.lowercase }}:${{ matrix.name }}-${{steps.version.outputs.result}}
          build-args: |
            SHA=${{ steps.version.outputs.result }}
          outputs: type=image,push=true
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache

      - name: Save digest
        if: github.ref == 'refs/heads/master'
        run: echo ${{ steps.build.outputs.digest }} > /tmp/digest.txt

      - name: Save release digest
        if: startsWith(github.ref, 'refs/tags/')
        run: echo ${{ steps.build_rel.outputs.digest }} > /tmp/digest.txt

      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: digest_${{ matrix.name }}
          path: /tmp/digest.txt

  manifests:
    name: Build manifests
    needs: [docker_build]
    runs-on: ubuntu-latest
    steps:
      - name: Get lowercase string for the repository name
        id: lowercase-repo-name
        uses: ASzc/change-string-case-action@v2
        with:
          string: ${{ github.event.repository.name }}

      - name: Checkout base
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      # https://github.com/docker/setup-qemu-action
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      # https://github.com/docker/setup-buildx-action
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
        with:
          config-inline: |
            [worker.oci]
              max-parallelism = 1

      - name: Download artifact
        uses: actions/download-artifact@v4
        with:
          path: /tmp/images/

      - name: Docker login
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Replace tag without `v`
        if: startsWith(github.ref, 'refs/tags/')
        uses: actions/github-script@v1
        id: version
        with:
          script: |
            return context.payload.ref.replace(/\/?refs\/tags\/v/, '')
          result-encoding: string

      - name: Merge and push manifest on master branch
        if: github.ref == 'refs/heads/master'
        run: python scripts/merge_manifest.py "${{ secrets.DOCKER_USERNAME }}/${{ steps.lowercase-repo-name.outputs.lowercase }}"

      - name: Merge and push manifest on release
        if: startsWith(github.ref, 'refs/tags/')
        run: python scripts/merge_manifest.py "${{ secrets.DOCKER_USERNAME }}/${{ steps.lowercase-repo-name.outputs.lowercase }}" ${{steps.version.outputs.result}}