Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as opaque submodule gitlinks on GitHub) into fully-tracked first-party content. AITM now merges its 7 vendored repos as first-party engines: - memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts - git: vendored C git (bundled binary) + gitnexus (in-process code intelligence) - agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess) Includes the full session's product build: Gitgram web/mobile/backend features, AITM website/CLI/MCP, the unified memory layer, engine build tooling (scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md), and extensive test suites. Build artifacts, node_modules, external/, and gemini-cli are gitignored. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
674 lines
27 KiB
YAML
674 lines
27 KiB
YAML
# =============================================================================
|
|
# Deploy AITimeMachine - API Server + Workers
|
|
# =============================================================================
|
|
# Triggered by:
|
|
# - Push to main (auto-deploy to staging, then production with approval)
|
|
# - Version tag (auto-deploy to production)
|
|
# - Manual workflow_dispatch
|
|
#
|
|
# Strategy:
|
|
# - Rolling deployment with health check validation
|
|
# - Automatic rollback on health check failure
|
|
# - Pre-deploy gate: build + test + migration dry-run
|
|
# - Post-deploy gate: smoke tests
|
|
# =============================================================================
|
|
name: Deploy AITM
|
|
|
|
on:
|
|
push:
|
|
branches: [main, master]
|
|
tags: ["v*"]
|
|
paths:
|
|
- "api-server/**"
|
|
- "drizzle/**"
|
|
- "src/**"
|
|
- ".github/workflows/deploy.yml"
|
|
workflow_dispatch:
|
|
inputs:
|
|
environment:
|
|
description: "Target environment"
|
|
type: choice
|
|
options:
|
|
- staging
|
|
- production
|
|
default: staging
|
|
skip_tests:
|
|
description: "Skip pre-deploy tests (emergency only)"
|
|
type: boolean
|
|
default: false
|
|
deploy_workers:
|
|
description: "Also deploy background workers"
|
|
type: boolean
|
|
default: true
|
|
|
|
concurrency:
|
|
group: deploy-aitm-${{ github.event.inputs.environment || (startsWith(github.ref, 'refs/tags/v') && 'production') || 'staging' }}
|
|
cancel-in-progress: false
|
|
|
|
env:
|
|
NODE_VERSION: "20"
|
|
DOCKER_REGISTRY: ghcr.io
|
|
DOCKER_IMAGE_API: ghcr.io/${{ github.repository }}/aitm-api
|
|
HEALTH_CHECK_RETRIES: 15
|
|
HEALTH_CHECK_INTERVAL: 10
|
|
|
|
jobs:
|
|
# ---------------------------------------------------------------------------
|
|
# Determine deployment target
|
|
# ---------------------------------------------------------------------------
|
|
resolve-environment:
|
|
name: Resolve Environment
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 2
|
|
outputs:
|
|
environment: ${{ steps.resolve.outputs.environment }}
|
|
is_production: ${{ steps.resolve.outputs.is_production }}
|
|
image_tag: ${{ steps.resolve.outputs.image_tag }}
|
|
deploy_workers: ${{ steps.resolve.outputs.deploy_workers }}
|
|
steps:
|
|
- name: Resolve deployment environment
|
|
id: resolve
|
|
run: |
|
|
if [ -n "${{ github.event.inputs.environment }}" ]; then
|
|
ENV="${{ github.event.inputs.environment }}"
|
|
elif [[ "${{ github.ref }}" == refs/tags/v* ]]; then
|
|
ENV="production"
|
|
else
|
|
ENV="staging"
|
|
fi
|
|
|
|
IS_PROD="false"
|
|
if [ "$ENV" = "production" ]; then
|
|
IS_PROD="true"
|
|
fi
|
|
|
|
# Image tag
|
|
if [[ "${{ github.ref }}" == refs/tags/v* ]]; then
|
|
IMAGE_TAG="${{ github.ref_name }}"
|
|
else
|
|
IMAGE_TAG="sha-$(echo ${{ github.sha }} | cut -c1-7)"
|
|
fi
|
|
|
|
# Workers flag
|
|
DEPLOY_WORKERS="${{ github.event.inputs.deploy_workers || 'true' }}"
|
|
|
|
echo "environment=$ENV" >> "$GITHUB_OUTPUT"
|
|
echo "is_production=$IS_PROD" >> "$GITHUB_OUTPUT"
|
|
echo "image_tag=$IMAGE_TAG" >> "$GITHUB_OUTPUT"
|
|
echo "deploy_workers=$DEPLOY_WORKERS" >> "$GITHUB_OUTPUT"
|
|
|
|
echo "### AITM Deploy Configuration" >> $GITHUB_STEP_SUMMARY
|
|
echo "| Setting | Value |" >> $GITHUB_STEP_SUMMARY
|
|
echo "|---------|-------|" >> $GITHUB_STEP_SUMMARY
|
|
echo "| Environment | $ENV |" >> $GITHUB_STEP_SUMMARY
|
|
echo "| Image Tag | $IMAGE_TAG |" >> $GITHUB_STEP_SUMMARY
|
|
echo "| Deploy Workers | $DEPLOY_WORKERS |" >> $GITHUB_STEP_SUMMARY
|
|
echo "| Ref | ${{ github.sha }} |" >> $GITHUB_STEP_SUMMARY
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Pre-deploy checks
|
|
# ---------------------------------------------------------------------------
|
|
pre-deploy-checks:
|
|
name: Pre-Deploy Checks
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 10
|
|
needs: [resolve-environment]
|
|
if: ${{ github.event.inputs.skip_tests != 'true' }}
|
|
defaults:
|
|
run:
|
|
working-directory: api-server
|
|
steps:
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Setup Node.js
|
|
uses: actions/setup-node@v4
|
|
with:
|
|
node-version: ${{ env.NODE_VERSION }}
|
|
cache: npm
|
|
cache-dependency-path: api-server/package-lock.json
|
|
|
|
- name: Install dependencies
|
|
run: npm ci
|
|
|
|
- name: TypeScript type check
|
|
run: npx tsc --noEmit
|
|
|
|
- name: Build
|
|
run: npm run build
|
|
|
|
- name: Run tests
|
|
run: npm test --if-present
|
|
env:
|
|
NODE_ENV: test
|
|
|
|
- name: Validate database migrations
|
|
run: |
|
|
echo "Checking drizzle migration files..."
|
|
if [ -d "drizzle" ]; then
|
|
MIGRATION_COUNT=$(ls drizzle/*.sql 2>/dev/null | wc -l)
|
|
echo "Found $MIGRATION_COUNT migration file(s)."
|
|
|
|
# Verify migration file syntax (basic SQL parse check)
|
|
for f in drizzle/*.sql; do
|
|
if [ -f "$f" ]; then
|
|
echo "Validating: $f"
|
|
# Check for common SQL issues
|
|
if grep -qi "DROP TABLE" "$f" && [ "${{ needs.resolve-environment.outputs.is_production }}" = "true" ]; then
|
|
echo "WARNING: DROP TABLE detected in $f - review carefully for production deploy"
|
|
fi
|
|
fi
|
|
done
|
|
else
|
|
echo "No drizzle migration directory found."
|
|
fi
|
|
|
|
- name: Check for breaking API changes
|
|
run: |
|
|
echo "Checking for potential breaking API changes..."
|
|
if [ -f "docs/openapi.yaml" ]; then
|
|
CHANGED=$(git diff --name-only HEAD~1 HEAD -- 'docs/openapi.yaml' 2>/dev/null || true)
|
|
if [ -n "$CHANGED" ]; then
|
|
echo "OpenAPI spec has been modified. Review for breaking changes."
|
|
git diff HEAD~1 HEAD -- 'docs/openapi.yaml' | head -100
|
|
fi
|
|
fi
|
|
|
|
# Check for removed route handlers
|
|
REMOVED_ROUTES=$(git diff HEAD~1 HEAD -- 'src/**/*.ts' | grep "^-.*\(app\|router\)\.\(get\|post\|put\|patch\|delete\)" 2>/dev/null || true)
|
|
if [ -n "$REMOVED_ROUTES" ]; then
|
|
echo "WARNING: Potentially removed route handlers:"
|
|
echo "$REMOVED_ROUTES"
|
|
fi
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Build Docker image
|
|
# ---------------------------------------------------------------------------
|
|
build-image:
|
|
name: Build Docker Image
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 15
|
|
needs: [resolve-environment, pre-deploy-checks]
|
|
if: always() && (needs.pre-deploy-checks.result == 'success' || needs.pre-deploy-checks.result == 'skipped')
|
|
permissions:
|
|
contents: read
|
|
packages: write
|
|
steps:
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
- name: Login to GitHub Container Registry
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ env.DOCKER_REGISTRY }}
|
|
username: ${{ github.actor }}
|
|
password: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
- name: Build and push API server image
|
|
uses: docker/build-push-action@v5
|
|
with:
|
|
context: ./api-server
|
|
file: ./api-server/Dockerfile
|
|
push: true
|
|
tags: |
|
|
${{ env.DOCKER_IMAGE_API }}:${{ needs.resolve-environment.outputs.image_tag }}
|
|
${{ env.DOCKER_IMAGE_API }}:${{ needs.resolve-environment.outputs.environment }}-latest
|
|
cache-from: type=gha
|
|
cache-to: type=gha,mode=max
|
|
labels: |
|
|
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
|
|
org.opencontainers.image.revision=${{ github.sha }}
|
|
deploy.environment=${{ needs.resolve-environment.outputs.environment }}
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Deploy to staging
|
|
# ---------------------------------------------------------------------------
|
|
deploy-staging:
|
|
name: Deploy to Staging
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 15
|
|
needs: [resolve-environment, build-image]
|
|
if: needs.resolve-environment.outputs.environment == 'staging'
|
|
environment:
|
|
name: staging
|
|
url: ${{ vars.AITM_STAGING_URL }}
|
|
steps:
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Record previous deployment version
|
|
id: previous
|
|
run: |
|
|
PREV_TAG="${{ vars.CURRENT_DEPLOY_TAG || 'none' }}"
|
|
echo "previous_tag=$PREV_TAG" >> "$GITHUB_OUTPUT"
|
|
echo "Previous deployment tag: $PREV_TAG"
|
|
|
|
- name: Run database migrations
|
|
run: |
|
|
echo "Running drizzle migrations on staging database..."
|
|
docker run --rm \
|
|
-e DATABASE_URL="${{ secrets.AITM_DATABASE_URL }}" \
|
|
${{ env.DOCKER_IMAGE_API }}:${{ needs.resolve-environment.outputs.image_tag }} \
|
|
npm run db:migrate
|
|
echo "Migrations complete."
|
|
|
|
- name: Deploy API server
|
|
run: |
|
|
echo "Deploying AITM API to staging..."
|
|
IMAGE_TAG="${{ needs.resolve-environment.outputs.image_tag }}"
|
|
|
|
if [ -n "${{ secrets.DEPLOY_SSH_KEY }}" ]; then
|
|
echo "${{ secrets.DEPLOY_SSH_KEY }}" > /tmp/deploy_key
|
|
chmod 600 /tmp/deploy_key
|
|
ssh -o StrictHostKeyChecking=no -i /tmp/deploy_key \
|
|
${{ secrets.STAGING_SSH_USER }}@${{ secrets.STAGING_SSH_HOST }} \
|
|
"docker pull ${{ env.DOCKER_IMAGE_API }}:$IMAGE_TAG && \
|
|
cd /opt/aitm && \
|
|
API_IMAGE=${{ env.DOCKER_IMAGE_API }}:$IMAGE_TAG \
|
|
ENABLE_WORKERS=${{ needs.resolve-environment.outputs.deploy_workers == 'true' && 'true' || 'false' }} \
|
|
RETENTION_CRON='${{ vars.AITM_RETENTION_CRON || '0 3 * * *' }}' \
|
|
DELETE_CRON='${{ vars.AITM_DELETE_CRON || '*/5 * * * *' }}' \
|
|
GC_CRON='${{ vars.AITM_GC_CRON || '0 4 * * 0' }}' \
|
|
docker compose up -d --remove-orphans aitm-api"
|
|
rm -f /tmp/deploy_key
|
|
else
|
|
echo "No SSH key configured. Using placeholder deployment."
|
|
echo "Configure DEPLOY_SSH_KEY, STAGING_SSH_USER, and STAGING_SSH_HOST secrets."
|
|
fi
|
|
|
|
- name: Deploy workers
|
|
if: needs.resolve-environment.outputs.deploy_workers == 'true'
|
|
run: |
|
|
echo "Deploying AITM background workers to staging..."
|
|
IMAGE_TAG="${{ needs.resolve-environment.outputs.image_tag }}"
|
|
|
|
if [ -n "${{ secrets.DEPLOY_SSH_KEY }}" ]; then
|
|
echo "${{ secrets.DEPLOY_SSH_KEY }}" > /tmp/deploy_key
|
|
chmod 600 /tmp/deploy_key
|
|
|
|
# Deploy delete-request worker
|
|
ssh -o StrictHostKeyChecking=no -i /tmp/deploy_key \
|
|
${{ secrets.STAGING_SSH_USER }}@${{ secrets.STAGING_SSH_HOST }} \
|
|
"cd /opt/aitm && \
|
|
API_IMAGE=${{ env.DOCKER_IMAGE_API }}:$IMAGE_TAG \
|
|
ENABLE_WORKERS=true \
|
|
DELETE_WORKER_ENABLED=true \
|
|
RETENTION_WORKER_ENABLED=true \
|
|
GC_WORKER_ENABLED=true \
|
|
RETENTION_CRON='${{ vars.AITM_RETENTION_CRON || '0 3 * * *' }}' \
|
|
DELETE_CRON='${{ vars.AITM_DELETE_CRON || '*/5 * * * *' }}' \
|
|
GC_CRON='${{ vars.AITM_GC_CRON || '0 4 * * 0' }}' \
|
|
docker compose up -d --remove-orphans aitm-worker-delete aitm-worker-retention"
|
|
rm -f /tmp/deploy_key
|
|
fi
|
|
|
|
- name: Health check - API server
|
|
id: health_api
|
|
run: |
|
|
API_URL="${{ vars.AITM_STAGING_URL || 'http://localhost:3001' }}"
|
|
echo "Running health checks against $API_URL..."
|
|
|
|
SUCCESS=false
|
|
for i in $(seq 1 ${{ env.HEALTH_CHECK_RETRIES }}); do
|
|
echo "Health check attempt $i/${{ env.HEALTH_CHECK_RETRIES }}..."
|
|
HTTP_CODE=$(curl -sf -o /dev/null -w "%{http_code}" "$API_URL/health" 2>/dev/null || echo "000")
|
|
|
|
if [ "$HTTP_CODE" = "200" ]; then
|
|
echo "API health check PASSED (HTTP $HTTP_CODE)"
|
|
SUCCESS=true
|
|
break
|
|
fi
|
|
|
|
echo "API not ready (HTTP $HTTP_CODE). Waiting ${{ env.HEALTH_CHECK_INTERVAL }}s..."
|
|
sleep ${{ env.HEALTH_CHECK_INTERVAL }}
|
|
done
|
|
|
|
if [ "$SUCCESS" = "false" ]; then
|
|
echo "health_passed=false" >> "$GITHUB_OUTPUT"
|
|
echo "API health check FAILED after ${{ env.HEALTH_CHECK_RETRIES }} attempts."
|
|
else
|
|
echo "health_passed=true" >> "$GITHUB_OUTPUT"
|
|
fi
|
|
|
|
- name: Health check - managed workers
|
|
if: needs.resolve-environment.outputs.deploy_workers == 'true'
|
|
id: health_workers
|
|
run: |
|
|
API_URL="${{ vars.AITM_STAGING_URL || 'http://localhost:3001' }}"
|
|
INTERNAL_SECRET="${{ secrets.AITM_INTERNAL_SECRET }}"
|
|
|
|
if [ -z "$INTERNAL_SECRET" ]; then
|
|
echo "health_passed=false" >> "$GITHUB_OUTPUT"
|
|
echo "AITM_INTERNAL_SECRET is required for worker status checks."
|
|
exit 0
|
|
fi
|
|
|
|
echo "Checking managed worker scheduler status..."
|
|
BODY=$(curl -sf -H "x-internal-secret: $INTERNAL_SECRET" "$API_URL/v1/workers/status" || true)
|
|
if [ -z "$BODY" ]; then
|
|
echo "health_passed=false" >> "$GITHUB_OUTPUT"
|
|
echo "Worker status endpoint returned empty response."
|
|
exit 0
|
|
fi
|
|
|
|
echo "$BODY"
|
|
if echo "$BODY" | grep -q '"managed":[[:space:]]*true' && \
|
|
echo "$BODY" | grep -q '"name":"retention"' && \
|
|
echo "$BODY" | grep -q '"name":"delete"'; then
|
|
echo "health_passed=true" >> "$GITHUB_OUTPUT"
|
|
echo "Managed workers are active."
|
|
else
|
|
echo "health_passed=false" >> "$GITHUB_OUTPUT"
|
|
echo "Managed worker scheduler is not healthy."
|
|
fi
|
|
|
|
- name: Rollback on failure
|
|
if: steps.health_api.outputs.health_passed != 'true' || (needs.resolve-environment.outputs.deploy_workers == 'true' && steps.health_workers.outputs.health_passed != 'true')
|
|
run: |
|
|
PREV_TAG="${{ steps.previous.outputs.previous_tag }}"
|
|
echo "ROLLING BACK AITM to previous version: $PREV_TAG"
|
|
|
|
if [ "$PREV_TAG" = "none" ]; then
|
|
echo "No previous version to roll back to. Manual intervention required."
|
|
exit 1
|
|
fi
|
|
|
|
if [ -n "${{ secrets.DEPLOY_SSH_KEY }}" ]; then
|
|
echo "${{ secrets.DEPLOY_SSH_KEY }}" > /tmp/deploy_key
|
|
chmod 600 /tmp/deploy_key
|
|
ssh -o StrictHostKeyChecking=no -i /tmp/deploy_key \
|
|
${{ secrets.STAGING_SSH_USER }}@${{ secrets.STAGING_SSH_HOST }} \
|
|
"cd /opt/aitm && \
|
|
API_IMAGE=${{ env.DOCKER_IMAGE_API }}:$PREV_TAG \
|
|
docker compose up -d --remove-orphans"
|
|
rm -f /tmp/deploy_key
|
|
fi
|
|
|
|
echo "Rollback complete. Failing the job."
|
|
exit 1
|
|
|
|
- name: Update deployment tag
|
|
if: steps.health_api.outputs.health_passed == 'true' && (needs.resolve-environment.outputs.deploy_workers != 'true' || steps.health_workers.outputs.health_passed == 'true')
|
|
run: |
|
|
echo "Staging deployment successful. Tag: ${{ needs.resolve-environment.outputs.image_tag }}"
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Deploy to production
|
|
# ---------------------------------------------------------------------------
|
|
deploy-production:
|
|
name: Deploy to Production
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 20
|
|
needs: [resolve-environment, build-image]
|
|
if: needs.resolve-environment.outputs.environment == 'production'
|
|
environment:
|
|
name: production
|
|
url: ${{ vars.AITM_PROD_URL }}
|
|
steps:
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Record previous deployment version
|
|
id: previous
|
|
run: |
|
|
PREV_TAG="${{ vars.CURRENT_DEPLOY_TAG || 'none' }}"
|
|
echo "previous_tag=$PREV_TAG" >> "$GITHUB_OUTPUT"
|
|
echo "Previous production deployment tag: $PREV_TAG"
|
|
|
|
- name: Pre-deploy database backup
|
|
run: |
|
|
echo "Creating pre-deploy Neon database backup..."
|
|
# Neon: create a branch as a point-in-time backup
|
|
# neon branches create --name "pre-deploy-$(date +%Y%m%d-%H%M%S)" --project-id $PROJECT_ID
|
|
echo "Backup step complete (delegate to Neon branch or pg_dump)."
|
|
|
|
- name: Run database migrations
|
|
run: |
|
|
echo "Running drizzle migrations on production database..."
|
|
docker run --rm \
|
|
-e DATABASE_URL="${{ secrets.AITM_DATABASE_URL }}" \
|
|
${{ env.DOCKER_IMAGE_API }}:${{ needs.resolve-environment.outputs.image_tag }} \
|
|
npm run db:migrate
|
|
echo "Migrations complete."
|
|
|
|
- name: Deploy API server
|
|
run: |
|
|
echo "Deploying AITM API to production..."
|
|
IMAGE_TAG="${{ needs.resolve-environment.outputs.image_tag }}"
|
|
|
|
if [ -n "${{ secrets.DEPLOY_SSH_KEY }}" ]; then
|
|
echo "${{ secrets.DEPLOY_SSH_KEY }}" > /tmp/deploy_key
|
|
chmod 600 /tmp/deploy_key
|
|
ssh -o StrictHostKeyChecking=no -i /tmp/deploy_key \
|
|
${{ secrets.PROD_SSH_USER }}@${{ secrets.PROD_SSH_HOST }} \
|
|
"docker pull ${{ env.DOCKER_IMAGE_API }}:$IMAGE_TAG && \
|
|
cd /opt/aitm && \
|
|
API_IMAGE=${{ env.DOCKER_IMAGE_API }}:$IMAGE_TAG \
|
|
ENABLE_WORKERS=${{ needs.resolve-environment.outputs.deploy_workers == 'true' && 'true' || 'false' }} \
|
|
RETENTION_CRON='${{ vars.AITM_RETENTION_CRON || '0 3 * * *' }}' \
|
|
DELETE_CRON='${{ vars.AITM_DELETE_CRON || '*/5 * * * *' }}' \
|
|
GC_CRON='${{ vars.AITM_GC_CRON || '0 4 * * 0' }}' \
|
|
docker compose up -d --remove-orphans aitm-api"
|
|
rm -f /tmp/deploy_key
|
|
else
|
|
echo "No SSH key configured. Using placeholder deployment."
|
|
fi
|
|
|
|
- name: Deploy workers
|
|
if: needs.resolve-environment.outputs.deploy_workers == 'true'
|
|
run: |
|
|
echo "Deploying AITM background workers to production..."
|
|
IMAGE_TAG="${{ needs.resolve-environment.outputs.image_tag }}"
|
|
|
|
if [ -n "${{ secrets.DEPLOY_SSH_KEY }}" ]; then
|
|
echo "${{ secrets.DEPLOY_SSH_KEY }}" > /tmp/deploy_key
|
|
chmod 600 /tmp/deploy_key
|
|
ssh -o StrictHostKeyChecking=no -i /tmp/deploy_key \
|
|
${{ secrets.PROD_SSH_USER }}@${{ secrets.PROD_SSH_HOST }} \
|
|
"cd /opt/aitm && \
|
|
API_IMAGE=${{ env.DOCKER_IMAGE_API }}:$IMAGE_TAG \
|
|
ENABLE_WORKERS=true \
|
|
DELETE_WORKER_ENABLED=true \
|
|
RETENTION_WORKER_ENABLED=true \
|
|
GC_WORKER_ENABLED=true \
|
|
RETENTION_CRON='${{ vars.AITM_RETENTION_CRON || '0 3 * * *' }}' \
|
|
DELETE_CRON='${{ vars.AITM_DELETE_CRON || '*/5 * * * *' }}' \
|
|
GC_CRON='${{ vars.AITM_GC_CRON || '0 4 * * 0' }}' \
|
|
docker compose up -d --remove-orphans aitm-worker-delete aitm-worker-retention"
|
|
rm -f /tmp/deploy_key
|
|
fi
|
|
|
|
- name: Health check - API server
|
|
id: health_api
|
|
run: |
|
|
API_URL="${{ vars.AITM_PROD_URL || 'https://timemachine-api.gitgram.app' }}"
|
|
echo "Running production health checks against $API_URL..."
|
|
|
|
SUCCESS=false
|
|
for i in $(seq 1 ${{ env.HEALTH_CHECK_RETRIES }}); do
|
|
echo "Health check attempt $i/${{ env.HEALTH_CHECK_RETRIES }}..."
|
|
HTTP_CODE=$(curl -sf -o /dev/null -w "%{http_code}" "$API_URL/health" 2>/dev/null || echo "000")
|
|
|
|
if [ "$HTTP_CODE" = "200" ]; then
|
|
echo "API health check PASSED (HTTP $HTTP_CODE)"
|
|
SUCCESS=true
|
|
break
|
|
fi
|
|
|
|
echo "API not ready (HTTP $HTTP_CODE). Waiting ${{ env.HEALTH_CHECK_INTERVAL }}s..."
|
|
sleep ${{ env.HEALTH_CHECK_INTERVAL }}
|
|
done
|
|
|
|
if [ "$SUCCESS" = "false" ]; then
|
|
echo "health_passed=false" >> "$GITHUB_OUTPUT"
|
|
else
|
|
echo "health_passed=true" >> "$GITHUB_OUTPUT"
|
|
fi
|
|
|
|
- name: Health check - managed workers
|
|
if: needs.resolve-environment.outputs.deploy_workers == 'true'
|
|
id: health_workers
|
|
run: |
|
|
API_URL="${{ vars.AITM_PROD_URL || 'https://timemachine-api.gitgram.app' }}"
|
|
INTERNAL_SECRET="${{ secrets.AITM_INTERNAL_SECRET }}"
|
|
|
|
if [ -z "$INTERNAL_SECRET" ]; then
|
|
echo "health_passed=false" >> "$GITHUB_OUTPUT"
|
|
echo "AITM_INTERNAL_SECRET is required for worker status checks."
|
|
exit 0
|
|
fi
|
|
|
|
echo "Checking managed worker scheduler status..."
|
|
BODY=$(curl -sf -H "x-internal-secret: $INTERNAL_SECRET" "$API_URL/v1/workers/status" || true)
|
|
if [ -z "$BODY" ]; then
|
|
echo "health_passed=false" >> "$GITHUB_OUTPUT"
|
|
echo "Worker status endpoint returned empty response."
|
|
exit 0
|
|
fi
|
|
|
|
echo "$BODY"
|
|
if echo "$BODY" | grep -q '"managed":[[:space:]]*true' && \
|
|
echo "$BODY" | grep -q '"name":"retention"' && \
|
|
echo "$BODY" | grep -q '"name":"delete"'; then
|
|
echo "health_passed=true" >> "$GITHUB_OUTPUT"
|
|
echo "Managed workers are active."
|
|
else
|
|
echo "health_passed=false" >> "$GITHUB_OUTPUT"
|
|
echo "Managed worker scheduler is not healthy."
|
|
fi
|
|
|
|
- name: Automatic rollback on failure
|
|
if: steps.health_api.outputs.health_passed != 'true' || (needs.resolve-environment.outputs.deploy_workers == 'true' && steps.health_workers.outputs.health_passed != 'true')
|
|
run: |
|
|
PREV_TAG="${{ steps.previous.outputs.previous_tag }}"
|
|
echo "PRODUCTION ROLLBACK to version: $PREV_TAG"
|
|
|
|
if [ "$PREV_TAG" = "none" ]; then
|
|
echo "No previous version to roll back to. MANUAL INTERVENTION REQUIRED."
|
|
exit 1
|
|
fi
|
|
|
|
if [ -n "${{ secrets.DEPLOY_SSH_KEY }}" ]; then
|
|
echo "${{ secrets.DEPLOY_SSH_KEY }}" > /tmp/deploy_key
|
|
chmod 600 /tmp/deploy_key
|
|
ssh -o StrictHostKeyChecking=no -i /tmp/deploy_key \
|
|
${{ secrets.PROD_SSH_USER }}@${{ secrets.PROD_SSH_HOST }} \
|
|
"cd /opt/aitm && \
|
|
API_IMAGE=${{ env.DOCKER_IMAGE_API }}:$PREV_TAG \
|
|
docker compose up -d --remove-orphans"
|
|
rm -f /tmp/deploy_key
|
|
fi
|
|
|
|
echo "Rollback complete. Failing the job to alert the team."
|
|
exit 1
|
|
|
|
- name: Update deployment tag
|
|
if: steps.health_api.outputs.health_passed == 'true' && (needs.resolve-environment.outputs.deploy_workers != 'true' || steps.health_workers.outputs.health_passed == 'true')
|
|
run: |
|
|
echo "Production deployment successful. Tag: ${{ needs.resolve-environment.outputs.image_tag }}"
|
|
|
|
- name: Notify deployment success
|
|
if: steps.health_api.outputs.health_passed == 'true' && (needs.resolve-environment.outputs.deploy_workers != 'true' || steps.health_workers.outputs.health_passed == 'true')
|
|
run: |
|
|
echo "### AITM Production Deployment Complete" >> $GITHUB_STEP_SUMMARY
|
|
echo "" >> $GITHUB_STEP_SUMMARY
|
|
echo "- **Image Tag:** ${{ needs.resolve-environment.outputs.image_tag }}" >> $GITHUB_STEP_SUMMARY
|
|
echo "- **Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY
|
|
echo "- **Workers deployed:** ${{ needs.resolve-environment.outputs.deploy_workers }}" >> $GITHUB_STEP_SUMMARY
|
|
echo "- **Deployed at:** $(date -u +'%Y-%m-%d %H:%M:%S UTC')" >> $GITHUB_STEP_SUMMARY
|
|
|
|
if [ -n "${{ secrets.SLACK_WEBHOOK_URL }}" ]; then
|
|
curl -sf -X POST "${{ secrets.SLACK_WEBHOOK_URL }}" \
|
|
-H 'Content-Type: application/json' \
|
|
-d "{\"text\":\"AITM production deploy complete: ${{ needs.resolve-environment.outputs.image_tag }}\"}" \
|
|
|| echo "Slack notification failed (non-blocking)"
|
|
fi
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Post-deploy smoke tests
|
|
# ---------------------------------------------------------------------------
|
|
smoke-tests:
|
|
name: Post-Deploy Smoke Tests
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 10
|
|
needs: [resolve-environment, deploy-staging, deploy-production]
|
|
if: always() && (needs.deploy-staging.result == 'success' || needs.deploy-production.result == 'success')
|
|
steps:
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Run AITM smoke tests
|
|
run: |
|
|
ENV="${{ needs.resolve-environment.outputs.environment }}"
|
|
|
|
if [ "$ENV" = "production" ]; then
|
|
API_URL="${{ vars.AITM_PROD_URL || 'https://timemachine-api.gitgram.app' }}"
|
|
else
|
|
API_URL="${{ vars.AITM_STAGING_URL || 'https://api.aitimemachine.dev' }}"
|
|
fi
|
|
|
|
echo "Running AITM smoke tests for $ENV..."
|
|
echo "API: $API_URL"
|
|
|
|
FAILURES=0
|
|
|
|
# Test 1: Health endpoint
|
|
echo ""
|
|
echo "--- Test 1: Health endpoint ---"
|
|
HEALTH_CODE=$(curl -sf -o /dev/null -w "%{http_code}" "$API_URL/health" 2>/dev/null || echo "000")
|
|
if [ "$HEALTH_CODE" = "200" ]; then
|
|
echo "PASS: Health endpoint returned 200"
|
|
else
|
|
echo "FAIL: Health endpoint returned $HEALTH_CODE"
|
|
FAILURES=$((FAILURES + 1))
|
|
fi
|
|
|
|
# Test 2: Sessions endpoint (expect 401 unauthenticated, not 500)
|
|
echo ""
|
|
echo "--- Test 2: Sessions endpoint ---"
|
|
SESSIONS_CODE=$(curl -sf -o /dev/null -w "%{http_code}" "$API_URL/api/sessions" 2>/dev/null || echo "000")
|
|
if [ "$SESSIONS_CODE" != "500" ] && [ "$SESSIONS_CODE" != "000" ]; then
|
|
echo "PASS: Sessions endpoint returned $SESSIONS_CODE (not 500)"
|
|
else
|
|
echo "FAIL: Sessions endpoint returned $SESSIONS_CODE"
|
|
FAILURES=$((FAILURES + 1))
|
|
fi
|
|
|
|
# Test 3: Sync endpoint (expect 401 or 400, not 500)
|
|
echo ""
|
|
echo "--- Test 3: Sync endpoint ---"
|
|
SYNC_CODE=$(curl -sf -o /dev/null -w "%{http_code}" "$API_URL/api/sync" \
|
|
-X POST -H "Content-Type: application/json" -d '{}' 2>/dev/null || echo "000")
|
|
if [ "$SYNC_CODE" != "500" ] && [ "$SYNC_CODE" != "000" ]; then
|
|
echo "PASS: Sync endpoint returned $SYNC_CODE (not 500)"
|
|
else
|
|
echo "FAIL: Sync endpoint returned $SYNC_CODE"
|
|
FAILURES=$((FAILURES + 1))
|
|
fi
|
|
|
|
# Test 4: Keys endpoint (expect 401, not 500)
|
|
echo ""
|
|
echo "--- Test 4: Keys endpoint ---"
|
|
KEYS_CODE=$(curl -sf -o /dev/null -w "%{http_code}" "$API_URL/api/keys" 2>/dev/null || echo "000")
|
|
if [ "$KEYS_CODE" != "500" ] && [ "$KEYS_CODE" != "000" ]; then
|
|
echo "PASS: Keys endpoint returned $KEYS_CODE (not 500)"
|
|
else
|
|
echo "FAIL: Keys endpoint returned $KEYS_CODE"
|
|
FAILURES=$((FAILURES + 1))
|
|
fi
|
|
|
|
echo ""
|
|
echo "### AITM Smoke Test Results ($ENV)" >> $GITHUB_STEP_SUMMARY
|
|
echo "- API URL: $API_URL" >> $GITHUB_STEP_SUMMARY
|
|
echo "- Failures: $FAILURES" >> $GITHUB_STEP_SUMMARY
|
|
|
|
if [ "$FAILURES" -gt 0 ]; then
|
|
echo "SMOKE TESTS FAILED: $FAILURES failure(s)"
|
|
exit 1
|
|
fi
|
|
|
|
echo "All AITM smoke tests passed."
|