name: CI to Docker Hub

on:
  push:
    branches:
      - main
    paths:
      - .github/workflows/docker_build.yml
      - 'ComWeChatRobot/**'
      - 'wine/**'
      - 'Dockerfile'
      - 'VERSION'
      - '*.py'
      - 'WeChatHook.exe'

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          lfs: true
      
      - name: Checkout LFS objects
        run: git lfs checkout

      - name: init submodule
        run: git submodule update --init --recursive

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

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

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

      - uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_HUB_USERNAME }}
          password: ${{ secrets.DOCKER_HUB_TOKEN }}

      - name: Get Version Tag
        id: get_version_tag
        run: echo ::set-output name=VERSION::`cat VERSION`

      - name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          context: ./
          file: ./Dockerfile
          platforms: linux/arm64,linux/amd64
          builder: ${{ steps.buildx.outputs.name }}
          push: true
          tags: |
            ${{ secrets.DOCKER_HUB_USERNAME }}/docker-com_wechat_robot:latest
            ${{ secrets.DOCKER_HUB_USERNAME }}/docker-com_wechat_robot:${{ steps.get_version_tag.outputs.VERSION }}
          labels: |
            maintainer=${{ github.event.head_commit.author.email }}
            timestamp=${{ github.event.head_commit.timestamp }}
            message=${{ github.event.head_commit.message }}
            commit=${{ github.event.head_commit.url }}
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache

      - name: Image digest
        run: echo ${{ steps.docker_build.outputs.digest }}