name: build

on:
  push:
    branches:
    tags:
    paths:
      - ".github/workflows/ci-build.yml"
      - "backend/**"
      - "frontend/apps/**"
      - ".dockerignore"
      - "docker-init.sh"
      - "Dockerfile"
      - "!**.md"
      - "!frontend/packages/**"
  pull_request:
    paths:
      - ".github/workflows/ci-build.yml"
      - "backend/**"
      - "frontend/apps/**"
      - ".dockerignore"
      - "docker-init.sh"
      - "Dockerfile"
      - "!**.md"

jobs:
  build-images:
    name: Build Docker images
    runs-on: ubuntu-latest

    steps:
      - 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: available platforms
        run: echo ${{ steps.buildx.outputs.platforms }}

      - name: build docker image without pushing (only outside master)
        if: ${{ github.ref != 'refs/heads/master' }}
        run: |
          docker buildx build \
              --build-arg SKIP_BACKEND_TEST=true --build-arg SKIP_FRONTEND_TEST=true \
              --platform linux/amd64 .

      - name: build example docker image without pushing (only outside master)
        if: ${{ github.ref != 'refs/heads/master' }}
        run: |
          docker buildx build \
              --build-arg SKIP_BACKEND_TEST=true --build-arg SKIP_FRONTEND_TEST=true \
              --platform linux/amd64 -f backend/_example/memory_store/Dockerfile .

      - name: build and deploy master image to ghcr.io and dockerhub
        if: ${{ github.ref == 'refs/heads/master' }}
        env:
          GITHUB_PACKAGE_TOKEN: ${{ secrets.PKG_TOKEN }}
          DOCKER_HUB_TOKEN: ${{ secrets.DOCKER_HUB_TOKEN }}
          USERNAME: ${{ github.actor }}
          GITHUB_SHA: ${{ github.sha}}
          GITHUB_REF: ${{ github.ref}}
        run: |
          ref="$(echo ${GITHUB_REF} | cut -d'/' -f3)"
          echo "GITHUB_REF=${GITHUB_REF}, GITHUB_SHA=${GITHUB_SHA}, GIT_BRANCH=${ref}"
          echo ${GITHUB_PACKAGE_TOKEN} | docker login ghcr.io -u ${USERNAME} --password-stdin
          echo ${DOCKER_HUB_TOKEN} | docker login -u umputun --password-stdin
          docker buildx build --push \
              --build-arg SKIP_BACKEND_TEST=true --build-arg SKIP_FRONTEND_TEST=true --build-arg CI=github \
              --build-arg GITHUB_SHA=${GITHUB_SHA} --build-arg GIT_BRANCH=${ref} --build-arg GITHUB_REF=${GITHUB_REF} \
              --platform linux/amd64,linux/arm/v7,linux/arm64 \
              -t ghcr.io/umputun/remark42:${ref} -t umputun/remark42:${ref} .

      - name: deploy tagged (latest) to ghcr.io and dockerhub
        if: ${{ startsWith(github.ref, 'refs/tags/') }}
        env:
          GITHUB_PACKAGE_TOKEN: ${{ secrets.PKG_TOKEN }}
          DOCKER_HUB_TOKEN: ${{ secrets.DOCKER_HUB_TOKEN }}
          USERNAME: ${{ github.actor }}
          GITHUB_SHA: ${{ github.sha}}
          GITHUB_REF: ${{ github.ref}}
        run: |
          ref="$(echo ${GITHUB_REF} | cut -d'/' -f3)"
          echo "GITHUB_REF=${GITHUB_REF}, GITHUB_SHA=${GITHUB_SHA}, GIT_BRANCH=${ref}"
          echo ${GITHUB_PACKAGE_TOKEN} | docker login ghcr.io -u ${USERNAME} --password-stdin
          echo ${DOCKER_HUB_TOKEN} | docker login -u umputun --password-stdin
          docker buildx build --push \
              --build-arg SKIP_BACKEND_TEST=true --build-arg SKIP_FRONTEND_TEST=true --build-arg CI=github \
              --build-arg GITHUB_SHA=${GITHUB_SHA} --build-arg GIT_BRANCH=${ref} --build-arg GITHUB_REF=${GITHUB_REF} \
              --platform linux/amd64,linux/arm/v7,linux/arm64 \
              -t ghcr.io/umputun/remark42:${ref} -t ghcr.io/umputun/remark42:latest \
              -t umputun/remark42:${ref} -t umputun/remark42:latest .

      - name: remote deployment to remark42.com from master
        if: ${{ github.ref == 'refs/heads/master' }}
        env:
          UPDATER_KEY: ${{ secrets.UPDATER_KEY }}
        run: curl -s https://jess.umputun.com/update/remark42-core/${UPDATER_KEY}
