name: Docker
on:
  push:
    branches:
      - master
      - next
  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - context: ./back
            label: back
            image: ghcr.io/${{github.repository_owner}}/kyoo_back
          - context: ./front
            label: front
            image: ghcr.io/${{github.repository_owner}}/kyoo_front
    name: Docker build ${{matrix.label}}
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: recursive
          fetch-depth: 0

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: ${{matrix.image}}
          tags: |
            type=edge
            type=ref,event=branch
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}

      - name: Check if a package should be pushed
        run: |
          echo "SHOULD_PUSH=$([ "${GITHUB_REF##*/}" == "master" ] || [ "${GITHUB_REF##*/}" == "next" ] && echo "true" || echo "false")" >> $GITHUB_ENV

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

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

      - name: Login to DockerHub
        uses: docker/login-action@v1 
        with:
          registry: ghcr.io
          username: ${{github.repository_owner}}
          password: ${{secrets.GITHUB_TOKEN}}

      - name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          context: ${{matrix.context}}
          build-args: |
            VERSION=0.0.0
          push: ${{env.SHOULD_PUSH}}
          tags: ${{steps.meta.outputs.tags}}
