name: MITMproxy w/Healthcheck

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:
  mitmproxy-build:
    name: "MITM proxy with Healthchecks"
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      max-parallel: 15
      matrix:
        mitmproxy:
          - latest
          - 10.2.2
          - 10.2.1
          - 10.2.0
          - 10.1.6
          - 10.1.5
          - 10.1.4
          - 10.1.3
          - 10.1.2
          - 10.1.1
          - 10.1.0
          - 10.0.0
          - 9.0.1
          - 9.0.0
          - 8.1.1
          - 8.1.0
          - 8.0.0
          - 7.0.4
          - 7.0.3
          - 7.0.2
          - 7.0.1
          - 7.0.0
          - 6.0.2
          - 6.0.1
          - 6.0.0
          - 5.3.0
          - 5.2
          - 5.1.1
          - 5.1.0
          - 5.0.1
          - 5.0.0
    steps:
      - 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: Checkout Source"
        uses: actions/checkout@v4

      - 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: "Build: Build MITM proxy with healthchecks"
        uses: docker/build-push-action@v5
        with:
          context: .
          build-contexts: |
            mitmproxy:version=docker-image://mitmproxy/mitmproxy:${{ matrix.mitmproxy }}
          build-args: |
            MITMPROXY_VERSION=${{ matrix.mitmproxy }}
            GIT_SHA=${{ github.sha }}
            BUILD_DATE=${{ github.event.repository.pushed_at }}
          platforms: ${{ (!env.ACT && !(startsWith('6.',matrix.mitmproxy) || startsWith('5.', matrix.mitmproxy))) && 'linux/amd64,linux/arm64' || 'linux/amd64' }}
          pull: true
          push: true
          tags: |
            ${{ format('benzine/mitmproxy:{0}', matrix.mitmproxy) }}
            ${{ format('ghcr.io/benzine-framework/mitmproxy:{0}', matrix.mitmproxy) }}
          cache-from: ${{ !env.ACT && 'type=gha' || '' }}
          cache-to: ${{ !env.ACT && 'type=gha,mode=max' || '' }}