name: Build Swarm Connectivity Tester

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:
  swarm-connectivity-tester-build:
    name: Build Swarm Connectivity Tester
    runs-on: ubuntu-latest
    steps:
      - name: "Setup: Checkout Source"
        uses: actions/checkout@v4

      - 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@v2

      - 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 }}-swarm-connectivity-tester-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: ${{ runner.os }}-swarm-connectivity-tester-composer-

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

      - name: "Build: Build & Push Target Image"
        uses: docker/build-push-action@v5
        with:
          context: swarm-connectivity-tester
          target: connect-target
          platforms: ${{ !env.ACT && 'linux/amd64,linux/arm64' || 'linux/amd64' }}
          pull: true
          push: true
          tags: |
            ${{ !env.ACT && 'benzine/swarm-connectivity-tester:target' || '' }}
            ${{ !env.ACT && 'ghcr.io/benzine-framework/swarm-connectivity-tester:target' || 'ghcr.io/benzine-framework/swarm-connectivity-tester:target-devel' }}
          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
            php:nginx=docker-image://ghcr.io/benzine-framework/php:nginx-8.2

      - name: "Build: Build & Push Report Image"
        uses: docker/build-push-action@v5
        with:
          context: .
          target: connect-reporter
          platforms: ${{ !env.ACT && 'linux/amd64,linux/arm64' || 'linux/amd64' }}
          pull: true
          push: true
          tags: |
            ${{ !env.ACT && 'benzine/swarm-connectivity-tester:reporter' || '' }}
            ${{ !env.ACT && 'ghcr.io/benzine-framework/swarm-connectivity-tester:reporter' || 'ghcr.io/benzine-framework/swarm-connectivity-tester:reporter-devel' }}
          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
            php:nginx=docker-image://ghcr.io/benzine-framework/php:nginx-8.2