name: Docker builds

env:
  TARGET_PLATFORMS: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64

on:
  schedule:
    - cron: "0 10 * * *" # everyday at 10am
  pull_request:
    branches: master
  push:
    branches: master
    tags:
      - v*

jobs:
  build:
    name: Build and deploy docker images
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

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

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1
        with:
          platforms: ${{ env.TARGET_PLATFORMS }}

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

      - name: Prepare
        id: prepare
        run: |
          DOCKER_USERNAME=viktorstrate
          DOCKER_IMAGE=viktorstrate/photoview
          DOCKER_PLATFORMS=${TARGET_PLATFORMS}
          VERSION=edge

          if [[ $GITHUB_REF == refs/tags/* ]]; then
            VERSION=${GITHUB_REF#refs/tags/v}
          fi
          if [ "${{ github.event_name }}" = "schedule" ]; then
            VERSION=nightly
          fi

          TAGS="--tag ${DOCKER_IMAGE}:${VERSION}"
          if [[ $VERSION =~ ^(([0-9]{1,3})\.[0-9]{1,3})\.[0-9]{1,3}$ ]]; then
            VERSION_MINOR=${BASH_REMATCH[1]}
            VERSION_MAJOR=${BASH_REMATCH[2]}
            TAGS="$TAGS --tag ${DOCKER_IMAGE}:latest --tag ${DOCKER_IMAGE}:${VERSION_MINOR} --tag ${DOCKER_IMAGE}:${VERSION_MAJOR}"
          fi

          echo ::set-output name=docker_username::${DOCKER_USERNAME}
          echo ::set-output name=docker_image::${DOCKER_IMAGE}
          echo ::set-output name=version::${VERSION}
          echo ::set-output name=buildx_args::--platform ${DOCKER_PLATFORMS} \
            --progress=plain \
            --cache-from "type=local,src=/tmp/.buildx-cache" \
            --cache-to "type=local,dest=/tmp/.buildx-cache" \
            --build-arg VERSION=${VERSION} \
            --build-arg BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') \
            --build-arg VCS_REF=${GITHUB_SHA::8} \
            ${TAGS} --file Dockerfile .

      - name: Build Docker images
        run: |
          docker buildx build --output "type=image,push=false" ${{ steps.prepare.outputs.buildx_args }}

      - name: Docker Login
        if: success() && github.event_name != 'pull_request' && github.repository == 'photoview/photoview'
        env:
          DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
        run: |
          echo "${DOCKER_PASSWORD}" | docker login --username "${{ steps.prepare.outputs.docker_username }}" --password-stdin

      - name: Push to Docker Hub
        if: success() && github.event_name != 'pull_request' && github.repository == 'photoview/photoview'
        run: |
          docker buildx build --output "type=image,push=true" ${{ steps.prepare.outputs.buildx_args }}

      # - name: Docker Check Manifest
      #   if: always() && github.event_name != 'pull_request' && github.repository == 'photoview/photoview'
      #   run: |
      #     docker run --rm mplatform/mquery ${{ steps.prepare.outputs.docker_image }}:${{ steps.prepare.outputs.version }}

      - name: Clear
        if: always() && github.event_name != 'pull_request'
        run: |
          rm -f ${HOME}/.docker/config.json
