name: docker push
on: [push]
jobs:
  push_to_registry:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master
      - name: Docker meta
        if: startsWith(github.ref, 'refs/tags/')
        id: docker_meta
        uses: crazy-max/ghaction-docker-meta@v1
        with:
          images: ghcr.io/${{ github.repository }}
          tag-match: v(.*)
      - name: Set up QEMU
        if: startsWith(github.ref, 'refs/tags/')
        uses: docker/setup-qemu-action@v1
      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v1
      - name: Cache Docker layers
        if: startsWith(github.ref, 'refs/tags/')
        uses: actions/cache@v2
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-buildx-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-buildx-
      - name: Login to GitHub Container Registry
        if: startsWith(github.ref, 'refs/tags/')
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name:  Build and push
        uses: docker/build-push-action@v2
        if: startsWith(github.ref, 'refs/tags/')
        with:
          builder: ${{ steps.buildx.outputs.name }}
          platforms: linux/amd64,linux/arm64
          tags: ${{ steps.docker_meta.outputs.tags }}
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache
          push: true