---

name: Build and Deploy CI

on:
  push:
    branches: [ 'dev-**', 'pr-**', staging, master ]
    tags: [ '**' ]
  pull_request:
    branches: [ staging, master ]
  schedule:
    # At 02:30 on Saturday
    - cron:  '30 2 * * 6'

jobs:

  build_docker:

    name: Build Docker Image
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        flavour: ["debian", "arch"]

    steps:
    # Fetch shallow git repository
    - name: Checkout
      uses: actions/checkout@v2

    # Use QEMU to build
    - name: Set up QEMU
      if: success()
      id: qemu
      uses: docker/setup-qemu-action@v1

    # Use docker buildx to build the docker image
    - name: Build the Docker image
      uses: docker/setup-buildx-action@v1
      if: success()
      id: buildx
      with:
        version: latest

    # Generate 'prepare' build arguments to be retrieved later on
    - name: Prepare
      if: success()
      id: prepare
      run: |
        FLAVOUR=${{ matrix.flavour }}
        echo  "FLAVOUR='${FLAVOUR}'"
        echo  "GITHUB_REF='${GITHUB_REF}'"
        echo  "GITHUB_REPOSITORY='${GITHUB_REPOSITORY}'"
        DOCKER_IMAGE=docker.io/josh5/steam-headless
        VERSION_TAG=${GITHUB_REF#refs/*/}
        DOCKER_TAGS=""
        if [[ ${VERSION_TAG%/merge} == 'master' ]]; then
          if [[ ${FLAVOUR} == 'debian' ]]; then
            DOCKER_TAGS="${DOCKER_TAGS}${DOCKER_IMAGE}:latest,"
          fi
          DOCKER_TAGS="${DOCKER_TAGS}${DOCKER_IMAGE}:${FLAVOUR},"
        elif [[ ${VERSION_TAG%/merge} == 'staging' ]]; then
          DOCKER_TAGS="${DOCKER_TAGS}${DOCKER_IMAGE}:${FLAVOUR}-staging,"
        elif [[ ${VERSION_TAG%/merge} =~ "dev-"* ]]; then
          DOCKER_TAGS="${DOCKER_TAGS}${DOCKER_IMAGE}:${FLAVOUR}-${VERSION_TAG%/merge},"
        fi
        if [[ ${GITHUB_REF} == refs/tags/* ]]; then
          VERSION=${GITHUB_REF#refs/tags/}
          if [[ ${VERSION} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}[-\w]*$ ]]; then
            DOCKER_TAGS="${DOCKER_TAGS}${DOCKER_IMAGE}:${FLAVOUR}-${VERSION},"
            if [[ ${FLAVOUR} == 'debian' ]]; then
              DOCKER_TAGS="${DOCKER_TAGS}${DOCKER_IMAGE}:latest,"
            fi
          fi
        fi
        echo "DOCKER_TAGS='${DOCKER_TAGS}'"

        echo "Build: [$(date +"%F %T")] [${GITHUB_REF_NAME}] [${GITHUB_SHA}] [${FLAVOUR}]" > ./overlay/version.txt

        DOCKER_PUSH="true"
        if [[ ${DOCKER_IMAGE} != 'docker.io/josh5/steam-headless' ]]; then
          DOCKER_PUSH="false"
        fi
        if [[ ${VERSION_TAG%/merge} =~ "pr-"* ]]; then
          DOCKER_PUSH="false"
        fi
        if [[ ${VERSION_TAG%/merge} =~ ^[0-9]+$ ]]; then
          DOCKER_PUSH="false"
        fi
        if [[ "X${DOCKER_TAGS}" == "X" ]]; then
          DOCKER_PUSH="false"
        fi
        echo "DOCKER_PUSH='${DOCKER_PUSH}'"
        cat ./overlay/version.txt

        echo "docker_image=${DOCKER_IMAGE}" >> $GITHUB_OUTPUT
        echo "docker_tags=$(echo ${DOCKER_TAGS} | sed 's/,$//')" >> $GITHUB_OUTPUT
        echo "docker_platforms=linux/amd64" >> $GITHUB_OUTPUT
        echo "docker_push=${DOCKER_PUSH}" >> $GITHUB_OUTPUT

    # Cache the build
    - name: Cache Docker layers
      uses: actions/cache@v2
      id: cache
      with:
        path: /tmp/.buildx-cache
        key: ${{ runner.os }}-buildx-${{ matrix.flavour }}-${{ github.sha }}
        restore-keys: |
          ${{ runner.os }}-buildx-${{ matrix.flavour }}-
    
    # Login to Docker Hub
    - name: Login to Docker Hub
      if: success() && (startsWith(github.ref, 'refs/heads/') || startsWith(github.ref, 'refs/tags/'))
      uses: docker/login-action@v1
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}
    
    # Run docker build and push
    - name: Docker Build and Push
      if: success()
      uses: docker/build-push-action@v2
      with:
        context: .
        file: Dockerfile.${{ matrix.flavour }}
        pull: true
        platforms: ${{ steps.prepare.outputs.docker_platforms }}
        push: ${{ steps.prepare.outputs.docker_push }}
        tags: |
          ${{ steps.prepare.outputs.docker_tags }}
        cache-from: type=local,src=/tmp/.buildx-cache
        cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new

    # Keep only latest cache
    #   https://github.com/docker/build-push-action/issues/252
    #   https://github.com/moby/buildkit/issues/1896
    - name: Move cache
      if: always()
      run: |
        if [[ -e /tmp/.buildx-cache-new ]]; then
          echo "Cleaning up old cache..."
          rm -rf /tmp/.buildx-cache
          mv -v /tmp/.buildx-cache-new /tmp/.buildx-cache
        fi
