name: Publish Docker image
on:
  push:
    tags: [v*]
jobs:
  push_to_registries:
    name: Push Docker image to multiple registries
    runs-on: ubuntu-latest
    permissions:
      packages: write
      contents: read
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v3
        with:
          images: |
            zadam/trilium
            ghcr.io/zadam/trilium
          tags: |
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}-latest
            type=match,pattern=(\d+.\d+).\d+\-beta,enable=${{ endsWith(github.ref, 'beta') }},group=1,suffix=-latest
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
        with:
          install: true
      - name: Log in to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      - name: Log in to GitHub Docker Registry
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Create server-package.json
        run: cat package.json | grep -v electron > server-package.json
      - name: Build and Push
        uses: docker/build-push-action@v2.7.0
        with:
          context: .
          platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6
          push: true
          cache-from: type=registry,ref=zadam/trilium:buildcache
          cache-to: type=registry,ref=zadam/trilium:buildcache,mode=max
          tags: ${{ steps.meta.outputs.tags }}
