# Woodpecker CI step for {{COMPONENT_NAME}} React app
# Add this step to your .woodpecker.yml

build-{{COMPONENT_NAME}}:
  depends_on: [preflight]
  image: woodpeckerci/plugin-kaniko
  settings:
    registry: registry.threesix.ai
    repo: {{PROJECT_NAME}}/{{COMPONENT_NAME}}
    tags:
      - latest
      - ${CI_COMMIT_SHA:0:8}
    context: .
    dockerfile: apps/{{COMPONENT_NAME}}/Dockerfile
    cache: true
    skip-tls-verify: true
  when:
    branch: main
    event: push

verify-{{COMPONENT_NAME}}:
  depends_on: [build-{{COMPONENT_NAME}}]
  image: alpine/curl
  failure: ignore
  commands:
    - |
      TAG="${CI_COMMIT_SHA:0:8}"
      REPO="{{PROJECT_NAME}}/{{COMPONENT_NAME}}"
      REGISTRY="registry.threesix.ai"
      echo "==> Verifying image $REGISTRY/$REPO:$TAG exists in registry"
      HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \
        --insecure \
        "https://$REGISTRY/v2/$REPO/manifests/$TAG" \
        -H "Accept: application/vnd.docker.distribution.manifest.v2+json")
      if [ "$HTTP_CODE" = "200" ]; then
        echo "==> Image verified: $REGISTRY/$REPO:$TAG"
        exit 0
      elif [ "$HTTP_CODE" = "404" ]; then
        echo "==> WARNING: Image $REGISTRY/$REPO:$TAG not found in registry"
        echo "    Build may have failed. Deploy will be skipped."
        exit 1
      else
        echo "==> WARNING: Registry check returned HTTP $HTTP_CODE"
        exit 0
      fi
  when:
    branch: main
    event: push

deploy-{{COMPONENT_NAME}}:
  depends_on: [verify-{{COMPONENT_NAME}}]
  image: bitnami/kubectl:latest
  commands:
    - kubectl set image deployment/{{PROJECT_NAME}}-{{COMPONENT_NAME}} {{COMPONENT_NAME}}=registry.threesix.ai/{{PROJECT_NAME}}/{{COMPONENT_NAME}}:${CI_COMMIT_SHA:0:8} -n projects || echo "Deployment not found, skipping"
  when:
    branch: main
    event: push
