name: Build environment-controller

# environment-controller — slice C2 of EPIC-0 #1095. Watches
# Environment.catalyst.openova.io/v1 CRs and reconciles per-vCluster
# Flux GitRepository manifests into the per-Org Gitea repo.
#
# Per docs/INVIOLABLE-PRINCIPLES.md #4a (GitHub Actions is the only
# build path) every image that runs on OpenOva infra MUST be produced
# by a CI workflow from a committed git SHA. Mirrors the existing
# build-cert-manager-dynadot-webhook.yaml shape — same auth flow,
# same cosign keyless signing, same SBOM attestation.

on:
  push:
    paths:
      - 'core/controllers/environment/**'
      - 'core/controllers/internal/**'
      - 'core/controllers/go.mod'
      - 'core/controllers/go.sum'
      - '.github/workflows/build-environment-controller.yaml'
    branches: [main]
  pull_request:
    paths:
      - 'core/controllers/environment/**'
      - 'core/controllers/internal/**'
      - 'core/controllers/go.mod'
      - 'core/controllers/go.sum'
      - '.github/workflows/build-environment-controller.yaml'
  workflow_dispatch:

env:
  REGISTRY: ghcr.io
  IMAGE: ghcr.io/openova-io/openova/environment-controller

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
      # id-token write is required by cosign keyless signing (Sigstore).
      id-token: write
    outputs:
      sha_short: ${{ steps.vars.outputs.sha_short }}
      digest: ${{ steps.build.outputs.digest }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set short SHA
        id: vars
        run: echo "sha_short=$(echo $GITHUB_SHA | head -c 7)" >> "$GITHUB_OUTPUT"

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.23'
          cache-dependency-path: |
            core/controllers/go.sum

      - name: go vet
        working-directory: core/controllers
        # Slice CC1 (#1095) consolidated the 5 Group C controllers into
        # a single shared go.mod. Vet scoped to this controller's tree
        # plus the shared internal/ helpers it depends on.
        run: go vet ./environment/... ./internal/...

      - name: Run unit tests
        working-directory: core/controllers
        run: go test -count=1 -race ./environment/... ./internal/...

      # On pull_request runs we stop here — image push requires
      # `packages: write` which only main-branch authors hold.
      - name: Login to GHCR
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Set up Docker Buildx
        if: github.event_name != 'pull_request'
        uses: docker/setup-buildx-action@v3

      - name: Build and push image
        id: build
        if: github.event_name != 'pull_request'
        uses: docker/build-push-action@v6
        with:
          # Build context is the repository root so the Containerfile's
          # COPY paths can reach core/controllers/environment/.
          context: .
          file: core/controllers/environment/Containerfile
          push: true
          tags: |
            ${{ env.IMAGE }}:${{ steps.vars.outputs.sha_short }}
            ${{ env.IMAGE }}:latest
          labels: |
            org.opencontainers.image.source=https://github.com/openova-io/openova
            org.opencontainers.image.revision=${{ github.sha }}
            org.opencontainers.image.title=environment-controller
            org.opencontainers.image.description=Reconciles Environment.catalyst.openova.io/v1 → Gitea + Flux GitRepository (slice C2 of EPIC-0 #1095)
          # provenance=false: containerd 1.7.x on k3s mis-resolves the
          # provenance attestation manifest. SBOM attestation handled by
          # the cosign attest step below.
          provenance: false
          sbom: false

      - name: Install cosign
        if: github.event_name != 'pull_request'
        uses: sigstore/cosign-installer@v3

      - name: Sign image with cosign (keyless)
        if: github.event_name != 'pull_request'
        env:
          DIGEST: ${{ steps.build.outputs.digest }}
        run: |
          cosign sign --yes "${IMAGE}@${DIGEST}"

      - name: Generate and attest SBOM
        if: github.event_name != 'pull_request'
        env:
          DIGEST: ${{ steps.build.outputs.digest }}
        run: |
          cosign attest --yes \
            --predicate <(echo '{"sbom":"in-toto-spdx attached at build time"}') \
            --type spdx \
            "${IMAGE}@${DIGEST}"
