name: Build and push Docker images

on:
  push:
    branches:
      - main
    paths:
      - 'api/src/**'
      - 'api/prisma/**'
      - 'web/src/**'
      - 'api/Dockerfile'
      - 'web/Dockerfile'
      - '.github/build.yml'
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Get changed files
        id: changed-files-specific
        uses: tj-actions/changed-files@v35
        with:
          files: |
            api/src/**
            api/prisma/**
            api/Dockerfile

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

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

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        if: steps.changed-files-specific.outputs.any_changed == 'true'
        uses: docker/build-push-action@v3
        with:
          context: ./api
          file: ./api/Dockerfile
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ghcr.io/renzynx/bliss:latest
          cache-from: type=gha
          cache-to: type=gha,mode=max

  web:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Get changed files
        id: changed-files-specific
        uses: tj-actions/changed-files@v35
        with:
          files: |
            web/src/**
            web/public/**
            web/Dockerfile

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

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

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        if: steps.changed-files-specific.outputs.any_changed == 'true'
        uses: docker/build-push-action@v3
        with:
          context: ./web
          file: ./web/Dockerfile
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ghcr.io/renzynx/web:latest
          cache-from: type=gha
          cache-to: type=gha,mode=max
