mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-04-01 23:45:42 +00:00
365 lines
15 KiB
YAML
365 lines
15 KiB
YAML
name: Deploy
|
|
|
|
on:
|
|
push:
|
|
branches: [master]
|
|
paths-ignore:
|
|
- '**.md'
|
|
- 'LICENSE'
|
|
- '.gitignore'
|
|
- 'docs/**'
|
|
|
|
concurrency:
|
|
group: deploy
|
|
cancel-in-progress: true
|
|
|
|
jobs:
|
|
go-build:
|
|
runs-on: self-hosted
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- uses: actions/setup-go@v5
|
|
with:
|
|
go-version: '1.22'
|
|
|
|
- name: Build & test Go server (with coverage)
|
|
run: |
|
|
cd cmd/server
|
|
go build .
|
|
go test -coverprofile=server-coverage.out ./... 2>&1 | tee server-test.log
|
|
echo "--- Go Server Coverage ---"
|
|
go tool cover -func=server-coverage.out | tail -1
|
|
|
|
- name: Build & test Go ingestor (with coverage)
|
|
run: |
|
|
cd cmd/ingestor
|
|
go build .
|
|
go test -coverprofile=ingestor-coverage.out ./... 2>&1 | tee ingestor-test.log
|
|
echo "--- Go Ingestor Coverage ---"
|
|
go tool cover -func=ingestor-coverage.out | tail -1
|
|
|
|
- name: Generate Go coverage badges
|
|
if: always()
|
|
run: |
|
|
mkdir -p .badges
|
|
|
|
# Parse server coverage
|
|
SERVER_COV="0"
|
|
if [ -f cmd/server/server-coverage.out ]; then
|
|
SERVER_COV=$(cd cmd/server && go tool cover -func=server-coverage.out | tail -1 | grep -oP '[\d.]+(?=%)')
|
|
fi
|
|
SERVER_COLOR="red"
|
|
if [ "$(echo "$SERVER_COV >= 80" | bc -l 2>/dev/null)" = "1" ]; then
|
|
SERVER_COLOR="green"
|
|
elif [ "$(echo "$SERVER_COV >= 60" | bc -l 2>/dev/null)" = "1" ]; then
|
|
SERVER_COLOR="yellow"
|
|
fi
|
|
echo "{\"schemaVersion\":1,\"label\":\"go server coverage\",\"message\":\"${SERVER_COV}%\",\"color\":\"${SERVER_COLOR}\"}" > .badges/go-server-coverage.json
|
|
echo "Go server coverage: ${SERVER_COV}% (${SERVER_COLOR})"
|
|
|
|
# Parse ingestor coverage
|
|
INGESTOR_COV="0"
|
|
if [ -f cmd/ingestor/ingestor-coverage.out ]; then
|
|
INGESTOR_COV=$(cd cmd/ingestor && go tool cover -func=ingestor-coverage.out | tail -1 | grep -oP '[\d.]+(?=%)')
|
|
fi
|
|
INGESTOR_COLOR="red"
|
|
if [ "$(echo "$INGESTOR_COV >= 80" | bc -l 2>/dev/null)" = "1" ]; then
|
|
INGESTOR_COLOR="green"
|
|
elif [ "$(echo "$INGESTOR_COV >= 60" | bc -l 2>/dev/null)" = "1" ]; then
|
|
INGESTOR_COLOR="yellow"
|
|
fi
|
|
echo "{\"schemaVersion\":1,\"label\":\"go ingestor coverage\",\"message\":\"${INGESTOR_COV}%\",\"color\":\"${INGESTOR_COLOR}\"}" > .badges/go-ingestor-coverage.json
|
|
echo "Go ingestor coverage: ${INGESTOR_COV}% (${INGESTOR_COLOR})"
|
|
|
|
echo "## Go Coverage" >> $GITHUB_STEP_SUMMARY
|
|
echo "| Module | Coverage |" >> $GITHUB_STEP_SUMMARY
|
|
echo "|--------|----------|" >> $GITHUB_STEP_SUMMARY
|
|
echo "| Server | ${SERVER_COV}% |" >> $GITHUB_STEP_SUMMARY
|
|
echo "| Ingestor | ${INGESTOR_COV}% |" >> $GITHUB_STEP_SUMMARY
|
|
|
|
- name: Upload Go badges
|
|
if: always()
|
|
uses: actions/upload-artifact@v4
|
|
with:
|
|
name: go-badges
|
|
path: .badges/go-*.json
|
|
retention-days: 1
|
|
|
|
test:
|
|
needs: go-build
|
|
runs-on: self-hosted
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
with:
|
|
fetch-depth: 2
|
|
|
|
- uses: actions/setup-node@v4
|
|
with:
|
|
node-version: '22'
|
|
|
|
- name: Install dependencies
|
|
run: npm ci --production=false
|
|
|
|
- name: Detect changes
|
|
id: changes
|
|
run: |
|
|
BACKEND=$(git diff --name-only HEAD~1 | grep -cE '^(server|db|decoder|packet-store|server-helpers|iata-coords)\.js$' || true)
|
|
FRONTEND=$(git diff --name-only HEAD~1 | grep -cE '^public/' || true)
|
|
TESTS=$(git diff --name-only HEAD~1 | grep -cE '^test-|^tools/' || true)
|
|
CI=$(git diff --name-only HEAD~1 | grep -cE '\.github/|package\.json|test-all\.sh|scripts/' || true)
|
|
# If CI/test infra changed, run everything
|
|
if [ "$CI" -gt 0 ]; then BACKEND=1; FRONTEND=1; fi
|
|
# If test files changed, run everything
|
|
if [ "$TESTS" -gt 0 ]; then BACKEND=1; FRONTEND=1; fi
|
|
echo "backend=$([[ $BACKEND -gt 0 ]] && echo true || echo false)" >> $GITHUB_OUTPUT
|
|
echo "frontend=$([[ $FRONTEND -gt 0 ]] && echo true || echo false)" >> $GITHUB_OUTPUT
|
|
echo "Changes: backend=$BACKEND frontend=$FRONTEND tests=$TESTS ci=$CI"
|
|
|
|
- name: Backend tests + coverage
|
|
if: steps.changes.outputs.backend == 'true'
|
|
run: |
|
|
npx c8 --reporter=text-summary --reporter=text sh test-all.sh 2>&1 | tee test-output.txt
|
|
|
|
TOTAL_PASS=$(grep -oP '\d+(?= passed)' test-output.txt | awk '{s+=$1} END {print s}')
|
|
TOTAL_FAIL=$(grep -oP '\d+(?= failed)' test-output.txt | awk '{s+=$1} END {print s}')
|
|
BE_COVERAGE=$(grep 'Statements' test-output.txt | tail -1 | grep -oP '[\d.]+(?=%)')
|
|
|
|
mkdir -p .badges
|
|
BE_COLOR="red"
|
|
[ "$(echo "$BE_COVERAGE > 60" | bc -l 2>/dev/null)" = "1" ] && BE_COLOR="yellow"
|
|
[ "$(echo "$BE_COVERAGE > 80" | bc -l 2>/dev/null)" = "1" ] && BE_COLOR="brightgreen"
|
|
echo "{\"schemaVersion\":1,\"label\":\"backend tests\",\"message\":\"${TOTAL_PASS} passed\",\"color\":\"brightgreen\"}" > .badges/backend-tests.json
|
|
echo "{\"schemaVersion\":1,\"label\":\"backend coverage\",\"message\":\"${BE_COVERAGE}%\",\"color\":\"${BE_COLOR}\"}" > .badges/backend-coverage.json
|
|
|
|
echo "## Backend: ${TOTAL_PASS} tests, ${BE_COVERAGE}% coverage" >> $GITHUB_STEP_SUMMARY
|
|
|
|
- name: Backend tests (quick — no coverage)
|
|
if: steps.changes.outputs.backend == 'false'
|
|
run: npm run test:unit
|
|
|
|
- name: Install Playwright browser
|
|
if: steps.changes.outputs.frontend == 'true'
|
|
run: npx playwright install chromium --with-deps 2>/dev/null || true
|
|
|
|
- name: Instrument frontend JS
|
|
if: steps.changes.outputs.frontend == 'true'
|
|
run: sh scripts/instrument-frontend.sh
|
|
|
|
- name: Start test server (instrumented)
|
|
if: steps.changes.outputs.frontend == 'true'
|
|
run: |
|
|
COVERAGE=1 PORT=13581 node server.js &
|
|
echo $! > .server.pid
|
|
echo "Server PID: $(cat .server.pid)"
|
|
# Health-check poll loop (up to 30s)
|
|
for i in $(seq 1 30); do
|
|
if curl -sf http://localhost:13581/api/stats > /dev/null 2>&1; then
|
|
echo "Server ready after ${i}s"
|
|
break
|
|
fi
|
|
if [ "$i" -eq 30 ]; then
|
|
echo "Server failed to start within 30s"
|
|
exit 1
|
|
fi
|
|
sleep 1
|
|
done
|
|
|
|
- name: Run Playwright E2E tests
|
|
if: steps.changes.outputs.frontend == 'true'
|
|
run: BASE_URL=http://localhost:13581 node test-e2e-playwright.js 2>&1 | tee e2e-output.txt
|
|
|
|
- name: Extract coverage + generate report
|
|
if: always() && steps.changes.outputs.frontend == 'true'
|
|
run: |
|
|
BASE_URL=http://localhost:13581 node scripts/collect-frontend-coverage.js 2>&1 | tee fe-coverage-output.txt
|
|
|
|
E2E_PASS=$(grep -oP '[0-9]+(?=/)' e2e-output.txt | tail -1)
|
|
|
|
mkdir -p .badges
|
|
if [ -f .nyc_output/frontend-coverage.json ]; then
|
|
npx nyc report --reporter=text-summary --reporter=text 2>&1 | tee fe-report.txt
|
|
FE_COVERAGE=$(grep 'Statements' fe-report.txt | head -1 | grep -oP '[\d.]+(?=%)' || echo "0")
|
|
FE_COVERAGE=${FE_COVERAGE:-0}
|
|
FE_COLOR="red"
|
|
[ "$(echo "$FE_COVERAGE > 50" | bc -l 2>/dev/null)" = "1" ] && FE_COLOR="yellow"
|
|
[ "$(echo "$FE_COVERAGE > 80" | bc -l 2>/dev/null)" = "1" ] && FE_COLOR="brightgreen"
|
|
echo "{\"schemaVersion\":1,\"label\":\"frontend coverage\",\"message\":\"${FE_COVERAGE}%\",\"color\":\"${FE_COLOR}\"}" > .badges/frontend-coverage.json
|
|
echo "## Frontend: ${FE_COVERAGE}% coverage" >> $GITHUB_STEP_SUMMARY
|
|
fi
|
|
echo "{\"schemaVersion\":1,\"label\":\"frontend tests\",\"message\":\"${E2E_PASS:-0} E2E passed\",\"color\":\"brightgreen\"}" > .badges/frontend-tests.json
|
|
|
|
- name: Stop test server
|
|
if: always() && steps.changes.outputs.frontend == 'true'
|
|
run: |
|
|
if [ -f .server.pid ]; then
|
|
kill $(cat .server.pid) 2>/dev/null || true
|
|
rm -f .server.pid
|
|
echo "Server stopped"
|
|
fi
|
|
|
|
- name: Frontend E2E only (no coverage)
|
|
if: steps.changes.outputs.frontend == 'false'
|
|
run: |
|
|
fuser -k 13581/tcp 2>/dev/null || true
|
|
PORT=13581 node server.js &
|
|
SERVER_PID=$!
|
|
sleep 5
|
|
BASE_URL=http://localhost:13581 node test-e2e-playwright.js || true
|
|
kill $SERVER_PID 2>/dev/null || true
|
|
|
|
- name: Download Go coverage badges
|
|
if: always()
|
|
continue-on-error: true
|
|
uses: actions/download-artifact@v4
|
|
with:
|
|
name: go-badges
|
|
path: .badges/
|
|
|
|
- name: Publish badges
|
|
if: always()
|
|
continue-on-error: true
|
|
run: |
|
|
git config user.name "github-actions"
|
|
git config user.email "actions@github.com"
|
|
git remote set-url origin https://x-access-token:${{ github.token }}@github.com/${{ github.repository }}.git
|
|
git add .badges/ -f
|
|
git diff --cached --quiet || (git commit -m "ci: update test badges [skip ci]" && git push) || echo "Badge push failed"
|
|
|
|
deploy:
|
|
needs: [go-build, test]
|
|
runs-on: self-hosted
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- uses: actions/setup-node@v4
|
|
with:
|
|
node-version: '22'
|
|
|
|
- name: Validate JS
|
|
run: sh scripts/validate.sh
|
|
|
|
- name: Build image
|
|
run: |
|
|
echo "${GITHUB_SHA::7}" > .git-commit
|
|
docker build -t meshcore-analyzer:latest .
|
|
echo "Built $(git rev-parse --short HEAD)"
|
|
|
|
- name: Deploy to staging
|
|
run: |
|
|
set -e
|
|
|
|
# Source environment overrides from deploy dir or home
|
|
if [ -f /opt/meshcore-deploy/.env ]; then
|
|
set -a; source /opt/meshcore-deploy/.env; set +a
|
|
echo "Sourced /opt/meshcore-deploy/.env"
|
|
elif [ -f "$HOME/.env" ]; then
|
|
set -a; source "$HOME/.env"; set +a
|
|
echo "Sourced $HOME/.env"
|
|
else
|
|
echo "No .env found, using defaults"
|
|
fi
|
|
|
|
# Ensure data directories exist
|
|
STAGING_DIR="${STAGING_DATA_DIR:-$HOME/meshcore-staging-data}"
|
|
mkdir -p "${PROD_DATA_DIR:-$HOME/meshcore-data}" "$STAGING_DIR"
|
|
|
|
# Ensure staging has a Caddyfile (generate from template if missing)
|
|
if [ ! -f "$STAGING_DIR/Caddyfile" ]; then
|
|
cp docker/Caddyfile.staging "$STAGING_DIR/Caddyfile"
|
|
echo "Generated staging Caddyfile"
|
|
fi
|
|
|
|
# Ensure staging has a config.json (copy from prod if missing)
|
|
if [ ! -f "$STAGING_DIR/config.json" ]; then
|
|
PROD_DIR="${PROD_DATA_DIR:-$HOME/meshcore-data}"
|
|
if [ -f "$PROD_DIR/config.json" ]; then
|
|
cp "$PROD_DIR/config.json" "$STAGING_DIR/config.json"
|
|
elif [ -f /opt/meshcore-deploy/config.json ]; then
|
|
cp /opt/meshcore-deploy/config.json "$STAGING_DIR/config.json"
|
|
else
|
|
echo '{}' > "$STAGING_DIR/config.json"
|
|
echo "WARNING: No config.json found, created empty one"
|
|
fi
|
|
fi
|
|
|
|
# Copy compose file to deploy dir so manage.sh can use it
|
|
mkdir -p /opt/meshcore-deploy
|
|
cp docker-compose.yml /opt/meshcore-deploy/docker-compose.yml
|
|
|
|
# Run compose from the repo checkout (where docker-compose.yml lives)
|
|
docker rm -f meshcore-staging 2>/dev/null || true
|
|
docker compose --profile staging up -d staging
|
|
|
|
# Wait for Docker healthcheck — give it up to 5 minutes (big DB)
|
|
for i in $(seq 1 300); do
|
|
HEALTH=$(docker inspect meshcore-staging --format '{{.State.Health.Status}}' 2>/dev/null || echo "starting")
|
|
if [ "$HEALTH" = "healthy" ]; then
|
|
echo "Staging healthy after ${i}s"
|
|
break
|
|
fi
|
|
if [ "$i" -eq 300 ]; then
|
|
echo "Staging failed health check after 300s"
|
|
docker logs meshcore-staging --tail 50
|
|
exit 1
|
|
fi
|
|
sleep 1
|
|
done
|
|
|
|
- name: Smoke test staging
|
|
run: |
|
|
curl -f http://localhost:81/api/stats || exit 1
|
|
curl -f http://localhost:81/api/nodes || exit 1
|
|
echo "Staging smoke tests passed"
|
|
|
|
- name: Deploy Go staging
|
|
run: |
|
|
echo "Building Go staging image..."
|
|
echo "${GITHUB_SHA::7}" > .git-commit
|
|
APP_VERSION=$(node -p "require('./package.json').version") \
|
|
GIT_COMMIT="${GITHUB_SHA::7}" \
|
|
docker compose --profile staging-go build staging-go
|
|
|
|
echo "Replacing Go staging container..."
|
|
docker rm -f meshcore-staging-go 2>/dev/null || true
|
|
docker compose --profile staging-go up -d staging-go
|
|
|
|
# Wait for healthy — Go starts fast, 60s is generous
|
|
for i in $(seq 1 60); do
|
|
HEALTH=$(docker inspect meshcore-staging-go --format '{{.State.Health.Status}}' 2>/dev/null || echo "starting")
|
|
if [ "$HEALTH" = "healthy" ]; then
|
|
echo "Go staging healthy after ${i}s"
|
|
break
|
|
fi
|
|
if [ "$i" -eq 60 ]; then
|
|
echo "WARNING: Go staging did not become healthy within 60s"
|
|
docker logs meshcore-staging-go --tail 30
|
|
exit 1
|
|
fi
|
|
sleep 1
|
|
done
|
|
|
|
# Verify API responds with engine field
|
|
if curl -sf http://localhost:82/api/stats | grep -q engine; then
|
|
echo "Go staging verified — engine field present"
|
|
else
|
|
echo "WARNING: Go staging /api/stats did not return engine field"
|
|
exit 1
|
|
fi
|
|
|
|
- name: Promotion instructions
|
|
run: |
|
|
echo "## Staging Deployed ✓" >> $GITHUB_STEP_SUMMARY
|
|
echo "" >> $GITHUB_STEP_SUMMARY
|
|
echo "**Commit:** \`$(git rev-parse --short HEAD)\` — $(git log -1 --format=%s)" >> $GITHUB_STEP_SUMMARY
|
|
echo "" >> $GITHUB_STEP_SUMMARY
|
|
echo "**Node Staging:** http://<VM_HOST>:81" >> $GITHUB_STEP_SUMMARY
|
|
echo "**Go Staging:** http://<VM_HOST>:82" >> $GITHUB_STEP_SUMMARY
|
|
echo "" >> $GITHUB_STEP_SUMMARY
|
|
echo "To promote to production:" >> $GITHUB_STEP_SUMMARY
|
|
echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY
|
|
echo "ssh deploy@<VM_HOST>" >> $GITHUB_STEP_SUMMARY
|
|
echo "cd /opt/meshcore-deploy" >> $GITHUB_STEP_SUMMARY
|
|
echo "./manage.sh promote" >> $GITHUB_STEP_SUMMARY
|
|
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
|