name: Build Bouncer

permissions:
  contents: read
  packages: write

on:
  workflow_call:
  workflow_dispatch:
  push:
    branches:
      - main
  schedule:
    - cron: "0 14 * * 2" # 2pm Patch Tuesday

concurrency:
  group: ${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  build:
    name: Build Docker Swarm Loadbalancer
    runs-on: ubuntu-latest
    steps:
      - name: "Setup: Checkout Source"
        uses: actions/checkout@v4

      - name: "Setup: Get Date"
        id: date
        run: |
          {
            echo "datetime=$(date +'%Y-%m-%d %H:%M:%S')"
            echo "date=$(date +'%Y-%m-%d')"
            echo "time=$(date +'%H:%M:%S')"
            echo "container_build_datetime=$(date -u +'%Y-%m-%dT%H:%M:%S.%3NZ')"
          } >> "$GITHUB_OUTPUT"

      - name: "Setup: PHP"
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.2

      - name: "Setup: Setup QEMU"
        uses: docker/setup-qemu-action@v3

      - name: "Setup: Expose GitHub Runtime"
        uses: crazy-max/ghaction-github-runtime@v3

      - name: "Setup: Setup Docker Buildx"
        uses: docker/setup-buildx-action@v3

      - name: "Setup: Login to Docker Hub"
        uses: docker/login-action@v3
        with:
          username: matthewbaggett
          password: ${{ secrets.DOCKER_HUB_TOKEN }}

      - name: "Setup: Login to GHCR"
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: matthewbaggett
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: "Setup: Find Composer Cache Directory"
        id: composer-cache
        run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

      - name: "Setup: Composer Cache"
        uses: actions/cache@v4
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-bouncer-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: ${{ runner.os }}-bouncer-composer-

      - name: "Dependencies: Composer Install"
        run: composer install --ignore-platform-reqs

      - name: "Build: Build & Push Image"
        uses: docker/build-push-action@v5
        with:
          context: .
          target: bouncer
          platforms: ${{ !env.ACT && 'linux/amd64,linux/arm64' || 'linux/amd64' }}
          pull: true
          push: true
          build-args: |
            GIT_SHA=${{ github.sha }}
            GIT_BUILD_ID=${{ github.ref_name }}
            BUILD_DATE=${{ steps.date.outputs.container_build_datetime }}
            GIT_COMMIT_MESSAGE=${{ github.event.head_commit.message }}
          tags: |
            benzine/bouncer:latest
            ghcr.io/benzine-framework/bouncer:latest
          cache-from: ${{ !env.ACT && 'type=gha' || '' }}
          cache-to: ${{ !env.ACT && 'type=gha,mode=max' || '' }}
          build-contexts: |
            php:cli=docker-image://ghcr.io/benzine-framework/php:cli-8.2

      - name: "Post-Build: Validate build"
        shell: bash
        run: |
          docker \
            run \
              --rm \
              ghcr.io/benzine-framework/bouncer:latest \
                /usr/bin/install-report