name: Build Axon

on:
  push:
    paths: ['products/axon/**']
    branches: [main]
  workflow_dispatch:

env:
  REGISTRY: ghcr.io
  IMAGE: ghcr.io/openova-io/openova/axon

permissions:
  contents: read
  packages: write

jobs:
  build-axon:
    runs-on: ubuntu-latest
    outputs:
      sha_short: ${{ steps.vars.outputs.sha_short }}
    steps:
      - uses: actions/checkout@v4

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

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

      - name: Build image
        uses: docker/build-push-action@v6
        with:
          context: products/axon
          file: products/axon/Containerfile
          push: false
          load: true
          tags: ${{ env.IMAGE }}:test

      - name: Smoke test
        run: |
          # Verify container starts without crashing.
          # Pool warmup blocks without Claude auth, so we check the
          # process stays alive (no import/build errors) rather than
          # testing the health endpoint.
          docker run -d --name smoke \
            -e AXON_API_KEYS=sk-smoke-test \
            ${{ env.IMAGE }}:test
          sleep 5

          if ! docker ps --filter "name=smoke" --filter "status=running" -q | grep -q .; then
            echo "Container crashed:"
            docker logs smoke
            exit 1
          fi
          echo "Smoke test passed: container running (pool warmup in progress)"
          docker stop smoke

      - name: Push image
        uses: docker/build-push-action@v6
        with:
          context: products/axon
          file: products/axon/Containerfile
          push: true
          tags: |
            ${{ env.IMAGE }}:${{ steps.vars.outputs.sha_short }}
            ${{ env.IMAGE }}:latest
