name: Mirror CI Images

on:
  push:
    branches:
      - main
    paths:
      - .github/workflows/mirror-ci-images.yml
      - scripts/ci/ci-image-mirrors.json
      - scripts/ci/mirror-ci-images.py
  schedule:
    # Weekly sync to pick up upstream security patches.
    - cron: '0 7 * * MON'
  workflow_dispatch:
    inputs:
      filter:
        description: Optional image name or name:tag filter (empty syncs all).
        required: false
        default: ''
        type: string
      force:
        description: Re-publish even when destination digests already match upstream.
        required: false
        default: false
        type: boolean
      dry_run:
        description: Validate upstream images and plan without pushing to GHCR.
        required: false
        default: false
        type: boolean
  pull_request:
    paths:
      - .github/workflows/mirror-ci-images.yml
      - scripts/ci/ci-image-mirrors.json
      - scripts/ci/mirror-ci-images.py
      - scripts/ci/test_mirror_ci_images.py

permissions:
  contents: read

concurrency:
  group: mirror-ci-images-${{ github.ref }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
  validate:
    name: Validate mirror catalog and dry-run
    runs-on: ubuntu-latest
    timeout-minutes: 15
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false

      - name: Run mirror script unit tests
        run: python3 -m unittest discover -s scripts/ci -p test_mirror_ci_images.py -v

      - uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1

      - name: Validate upstream manifests (dry-run)
        env:
          MSB_MIRROR_SOURCE_REPO: ${{ github.server_url }}/${{ github.repository }}
          MIRROR_FILTER: ${{ inputs.filter }}
        run: |
          set -euo pipefail
          args=(--dry-run)
          if [[ -n "$MIRROR_FILTER" ]]; then
            args+=(--filter "$MIRROR_FILTER")
          fi
          python3 scripts/ci/mirror-ci-images.py "${args[@]}"

  publish:
    name: Publish CI image mirrors to GHCR
    needs: validate
    if: >-
      github.event_name != 'pull_request' &&
      github.ref == 'refs/heads/main' &&
      github.repository == 'superradcompany/microsandbox' &&
      inputs.dry_run != true
    runs-on: ubuntu-latest
    timeout-minutes: 20
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false

      - uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1

      - uses: docker/login-action@81c6f93d4bc991e510b342a00b419b3bb4efc5c6 # v4.7.0
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Sync mirrors to GHCR
        env:
          MSB_MIRROR_SOURCE_REPO: ${{ github.server_url }}/${{ github.repository }}
          MIRROR_FILTER: ${{ inputs.filter }}
          MIRROR_FORCE: ${{ inputs.force }}
        run: |
          set -euo pipefail
          args=()
          if [[ -n "$MIRROR_FILTER" ]]; then
            args+=(--filter "$MIRROR_FILTER")
          fi
          if [[ "$MIRROR_FORCE" == "true" ]]; then
            args+=(--force)
          fi
          python3 scripts/ci/mirror-ci-images.py "${args[@]}"
