name: Docker

on:
  workflow_dispatch:
  push:
    tags: ['v*']

permissions:
  contents: read
  packages: write
  id-token: write

jobs:
  build-amd64:
    # Self-hosted `lux-build` ARC pool on DOKS hanzo-k8s (max 20 runners,
    # scales 0→N on demand). Org-isolated: luxfi workflows must use this
    # pool (never the hanzoai pools). amd64 only — DOKS has no arm64 yet.
    runs-on: lux-build
    outputs:
      digest: ${{ steps.build.outputs.digest }}
    steps:
      - uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
        with:
          driver: docker-container
          driver-opts: |
            network=host

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

      - name: Image metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ghcr.io/luxfi/node
          # Semver-only policy: only `v*` git tags yield a published image
          # tag; every push gets an immutable `sha-<sha7>` for traceability.
          # No `:latest`, no `:main`, no floating tags ever. `flavor.latest`
          # has to be explicit-false; docker/metadata-action defaults to
          # `latest=auto` which silently emits `:latest` on tag pushes.
          flavor: |
            latest=false
          tags: |
            type=ref,event=tag
            type=sha,format=short,prefix=sha-

      - name: Resolve cross-repo PAT for private luxfi/* deps
        id: pat
        env:
          # The default GITHUB_TOKEN is repo-scoped and cannot read
          # private cross-repo deps like luxfi/corona or luxfi/evm. We
          # need a PAT with org-wide read scope. Order of preference:
          # 1) org GH_TOKEN (visibility=all, set on luxfi org since 2024)
          # 2) repo UNIVERSE_PAT (set on luxfi/node since 2026-04)
          # Either suffices; we just need ONE non-empty token. If both
          # are empty the build is going to fail at go mod download for
          # corona — fail fast here with a clear message.
          GH_TOKEN: ${{ secrets.GH_TOKEN }}
          UNIVERSE_PAT: ${{ secrets.UNIVERSE_PAT }}
        run: |
          tok="$GH_TOKEN"
          src="GH_TOKEN"
          if [ -z "$tok" ]; then
            tok="$UNIVERSE_PAT"
            src="UNIVERSE_PAT"
          fi
          if [ -z "$tok" ]; then
            echo "::error::Neither GH_TOKEN (org) nor UNIVERSE_PAT (repo) is set. Private luxfi/* deps cannot be resolved."
            exit 1
          fi
          echo "Using secret: $src (length=${#tok})"
          # Pass the resolved token to subsequent steps without exposing
          # the value. ::add-mask:: prevents accidental log leaks if the
          # value ever appears in later step output.
          echo "::add-mask::$tok"
          echo "token<<EOF" >> "$GITHUB_OUTPUT"
          echo "$tok" >> "$GITHUB_OUTPUT"
          echo "EOF" >> "$GITHUB_OUTPUT"

      - name: Build & push (amd64)
        id: build
        uses: docker/build-push-action@v6
        with:
          context: .
          file: Dockerfile
          platforms: linux/amd64
          push: true
          build-args: |
            CGO_ENABLED=0
          # Resolve private luxfi/* modules via git url.insteadOf in the
          # Dockerfile. Take the token value from the previous step's
          # output (it picked GH_TOKEN or fell back to UNIVERSE_PAT).
          secrets: |
            ghtok=${{ steps.pat.outputs.token }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          provenance: false
          cache-from: type=registry,ref=ghcr.io/luxfi/node:buildcache-amd64
          cache-to: type=registry,ref=ghcr.io/luxfi/node:buildcache-amd64,mode=max

  notify-universe:
    needs: build-amd64
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    steps:
      - uses: peter-evans/repository-dispatch@v4
        with:
          token: ${{ secrets.UNIVERSE_PAT }}
          repository: luxfi/universe
          event-type: image-published
          client-payload: |
            {
              "service": "node",
              "image": "ghcr.io/luxfi/node",
              "tag": "${{ github.ref_name }}",
              "sha": "${{ github.sha }}"
            }
