name: Build, Lint and Test
on:
  push:
    branches:
      - monorepo
  pull_request:
jobs:
  build:
    name: Build, Lint and Test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Setup pnpm
        uses: pnpm/action-setup@v3

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          cache: pnpm
          node-version-file: package.json

      - name: Install Dependencies
        run: pnpm install

      - name: Codegen
        run: pnpm -r codegen

      - name: Lint
        run: pnpm run lint

      - name: Build
        run: pnpm run build

      - name: Test
        run: pnpm test

  build-docker:
    name: Build Docker Images
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        target:
          - pd
          - schema-server
          - api-worker
          - api-master
          - website
          - discord-bot
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

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

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

      - name: Get Target
        id: get_target
        run: echo "target=$([[ ${{ matrix.target }} == 'website' ]] && echo 'website' || echo 'prod')" >> $GITHUB_OUTPUT

      - name: Build
        uses: docker/build-push-action@v6
        with:
          cache-from: type=gha
          cache-to: type=gha,mode=max
          target: ${{ steps.get_target.outputs.target }}
          build-args: SERVICE=${{ matrix.target }}
