name: Build Docker Container

on: push

jobs:
  build-container:
    name: Build ${{ matrix.name }} Container
    runs-on: ubuntu-latest
    if: github.repository_owner == 'TandoorRecipes'
    continue-on-error: ${{ matrix.continue-on-error }}
    permissions:
      contents: read
      packages: write
    strategy:
      matrix:
        include:
          # Standard build config
          - name: Standard
            dockerfile: Dockerfile
            platforms: linux/amd64,linux/arm64
            suffix: ""
            continue-on-error: false
          # Raspi build config
          - name: Raspi
            dockerfile: Dockerfile-raspi
            platforms: linux/arm/v7
            suffix: "-raspi"
            continue-on-error: true
    steps:
      - uses: actions/checkout@v3

      - name: Get version number
        id: get_version
        run: |
          if [[ "$GITHUB_REF" = refs/tags/* ]]; then
            echo "VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_OUTPUT
          elif [[ "$GITHUB_REF" = refs/heads/beta ]]; then
            echo VERSION=beta >> $GITHUB_OUTPUT
          else
            echo VERSION=develop >> $GITHUB_OUTPUT
          fi

      # Update Version number
      - name: Update version file
        uses: DamianReeves/write-file-action@v1.2
        with:
          path: recipes/version.py
          contents: |
            VERSION_NUMBER = '${{ steps.get_version.outputs.VERSION }}'
            BUILD_REF = '${{ github.sha }}'
          write-mode: overwrite

      # Build Vue frontend
      - uses: actions/setup-node@v3
        with:
          node-version: '18'
          cache: yarn
          cache-dependency-path: vue/yarn.lock
      - name: Install dependencies
        working-directory: ./vue
        run: yarn install --frozen-lockfile
      - name: Build dependencies
        working-directory: ./vue
        run: yarn build

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2
      - name: Set up Buildx
        uses: docker/setup-buildx-action@v2
      - name: Login to Docker Hub
        uses: docker/login-action@v2
        if: github.secret_source == 'Actions'
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2
        if: github.secret_source == 'Actions'
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ github.token }}
      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            vabene1111/recipes
            ghcr.io/TandoorRecipes/recipes
          flavor: |
            latest=false
            suffix=${{ matrix.suffix }}
          tags: |
            type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/') }}
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
            type=ref,event=branch
      - name: Build and Push
        uses: docker/build-push-action@v4
        with:
          context: .
          file: ${{ matrix.dockerfile }}
          pull: true
          push: ${{ github.secret_source == 'Actions' }}
          platforms: ${{ matrix.platforms }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

  notify-stable:
    name: Notify Stable
    runs-on: ubuntu-latest
    needs: build-container
    if: startsWith(github.ref, 'refs/tags/')
    steps:
      - name: Set tag name
        run: |
          # Strip "refs/tags/" prefix
          echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
      # Send stable discord notification
      - name: Discord notification
        env:
          DISCORD_WEBHOOK: ${{ secrets.DISCORD_RELEASE_WEBHOOK }}
        uses: Ilshidur/action-discord@0.3.2
        with:
          args: '🚀 Version {{ VERSION }} of tandoor has been released 🥳 Check it out https://github.com/vabene1111/recipes/releases/tag/{{ VERSION }}'

  notify-beta:
    name: Notify Beta
    runs-on: ubuntu-latest
    needs: build-container
    if: github.ref == 'refs/heads/beta'
    steps:
      # Send beta discord notification
      - name: Discord notification
        env:
          DISCORD_WEBHOOK: ${{ secrets.DISCORD_BETA_WEBHOOK }}
        uses: Ilshidur/action-discord@0.3.2
        with:
          args: '🚀 The BETA Image has been updated! 🥳'
