name: Publish Images

on:
  release:
    types: [published]
  workflow_dispatch:

concurrency:
  group: publish-images-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read
  packages: write

env:
  DOCKER_PLATFORMS: linux/amd64,linux/arm64

jobs:
  publish:
    runs-on: ubuntu-latest
    env:
      MIRROR_REGISTRY: ${{ secrets.MIRROR_REGISTRY || secrets.ALIYUN_REGISTRY }}
      MIRROR_NAMESPACE: ${{ secrets.MIRROR_NAMESPACE || secrets.ALIYUN_NAME_SPACE }}
      MIRROR_REGISTRY_USERNAME: ${{ secrets.MIRROR_REGISTRY_USERNAME || secrets.ALIYUN_REGISTRY_USER }}
      MIRROR_REGISTRY_PASSWORD: ${{ secrets.MIRROR_REGISTRY_PASSWORD || secrets.ALIYUN_REGISTRY_PASSWORD }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: server
            context: ./server
            dockerfile: ./server/Dockerfile
            image: ghcr.io/${{ github.repository_owner }}/skillhub-server
            mirror_image: skillhub-server
          - name: web
            context: ./web
            dockerfile: ./web/Dockerfile
            image: ghcr.io/${{ github.repository_owner }}/skillhub-web
            mirror_image: skillhub-web
          - name: scanner
            context: ./scanner
            dockerfile: ./scanner/Dockerfile
            image: ghcr.io/${{ github.repository_owner }}/skillhub-scanner
            mirror_image: skillhub-scanner

    steps:
      - name: Check out repository
        uses: actions/checkout@v4

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

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

      - name: Log in to GHCR
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Detect mirror configuration
        id: mirror
        shell: bash
        run: |
          if [[ -n "${MIRROR_REGISTRY}" && -n "${MIRROR_NAMESPACE}" && -n "${MIRROR_REGISTRY_USERNAME}" && -n "${MIRROR_REGISTRY_PASSWORD}" ]]; then
            echo "enabled=true" >> "$GITHUB_OUTPUT"
          else
            echo "enabled=false" >> "$GITHUB_OUTPUT"
          fi

      - name: Log in to mirror registry
        if: ${{ steps.mirror.outputs.enabled == 'true' }}
        shell: bash
        run: |
          echo "${MIRROR_REGISTRY_PASSWORD}" | docker login "${MIRROR_REGISTRY}" \
            --username "${MIRROR_REGISTRY_USERNAME}" \
            --password-stdin

      - name: Prepare image targets
        id: targets
        run: |
          {
            echo "images<<EOF"
            echo "${{ matrix.image }}"
            if [ "${{ steps.mirror.outputs.enabled }}" = "true" ]; then
              echo "${MIRROR_REGISTRY%/}/${MIRROR_NAMESPACE}/${{ matrix.mirror_image }}"
            fi
            echo "EOF"
          } >> "$GITHUB_OUTPUT"

      - name: Extract image metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ steps.targets.outputs.images }}
          tags: |
            type=raw,value=edge,enable={{is_default_branch}}
            type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') }}
            type=ref,event=tag
            type=sha,format=short,prefix=sha-
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}

      - name: Build and push ${{ matrix.name }}
        uses: docker/build-push-action@v6
        with:
          context: ${{ matrix.context }}
          file: ${{ matrix.dockerfile }}
          platforms: ${{ env.DOCKER_PLATFORMS }}
          push: true
          provenance: false
          sbom: false
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha,scope=${{ matrix.name }}
          cache-to: type=gha,mode=max,scope=${{ matrix.name }}

  mirror-runtime-images:
    name: Mirror Runtime Images
    runs-on: ubuntu-latest
    needs: publish
    env:
      MIRROR_REGISTRY: ${{ secrets.MIRROR_REGISTRY || secrets.ALIYUN_REGISTRY }}
      MIRROR_NAMESPACE: ${{ secrets.MIRROR_NAMESPACE || secrets.ALIYUN_NAME_SPACE }}
      MIRROR_REGISTRY_USERNAME: ${{ secrets.MIRROR_REGISTRY_USERNAME || secrets.ALIYUN_REGISTRY_USER }}
      MIRROR_REGISTRY_PASSWORD: ${{ secrets.MIRROR_REGISTRY_PASSWORD || secrets.ALIYUN_REGISTRY_PASSWORD }}

    steps:
      - name: Check out repository
        uses: actions/checkout@v4

      - name: Detect mirror configuration
        id: mirror
        shell: bash
        run: |
          if [[ -n "${MIRROR_REGISTRY}" && -n "${MIRROR_NAMESPACE}" && -n "${MIRROR_REGISTRY_USERNAME}" && -n "${MIRROR_REGISTRY_PASSWORD}" ]]; then
            echo "enabled=true" >> "$GITHUB_OUTPUT"
          else
            echo "enabled=false" >> "$GITHUB_OUTPUT"
          fi

      - name: Set up Docker Buildx
        if: ${{ steps.mirror.outputs.enabled == 'true' }}
        uses: docker/setup-buildx-action@v3

      - name: Log in to mirror registry
        if: ${{ steps.mirror.outputs.enabled == 'true' }}
        shell: bash
        run: |
          echo "${MIRROR_REGISTRY_PASSWORD}" | docker login "${MIRROR_REGISTRY}" \
            --username "${MIRROR_REGISTRY_USERNAME}" \
            --password-stdin

      - name: Mirror runtime dependency images
        if: ${{ steps.mirror.outputs.enabled == 'true' }}
        run: bash scripts/mirror-runtime-images.sh

      - name: Skip mirroring when registry secrets are not configured
        if: ${{ steps.mirror.outputs.enabled != 'true' }}
        run: echo "Mirror registry secrets are not configured; skipping runtime image mirroring."
