diff --git a/.gitea/workflows/deploy-production.yml b/.gitea/workflows/deploy-production.yml new file mode 100644 index 0000000..e7b3b40 --- /dev/null +++ b/.gitea/workflows/deploy-production.yml @@ -0,0 +1,155 @@ +# Gitea Actions — Production deployment (manual, PROMOTE gate). +# +# Required org/repo secrets: +# PROD_HOST, PROD_USER, PROD_SSH_KEY, PROD_SSH_KNOWN_HOSTS +# Optional: PROD_PORT (default 22), PROD_URL (external smoke) +# +# PROD_SSH_KNOWN_HOSTS: multiline `ssh-keyscan -p -H ` for the deploy target. +name: ai-news-writer-deploy-production + +on: + workflow_dispatch: + inputs: + confirmation: + description: 'Type "PROMOTE" to proceed with production deployment' + required: true + default: '' + deployment_notes: + description: 'Optional deployment notes (e.g., commit hash or reason)' + required: false + default: '' + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: false + +jobs: + validate-approval: + runs-on: ubuntu-latest + steps: + - name: Validate approval confirmation + run: | + if [ "${{ github.event.inputs.confirmation }}" != "PROMOTE" ]; then + echo "Production deployment requires explicit PROMOTE confirmation" + exit 1 + fi + echo "Approval gate passed" + + deploy-production: + needs: validate-approval + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + ref: main + + - name: Pre-deployment validation + run: | + echo "=== Pre-deployment checks ===" + echo "Branch: ${{ github.ref_name }}" + echo "Deployment notes: ${{ github.event.inputs.deployment_notes }}" + echo "Workflow: ${{ github.workflow }}" + echo "Run ID: ${{ github.run_id }}" + + - name: Deploy to production via SSH + env: + PROD_HOST: ${{ secrets.PROD_HOST }} + PROD_USER: ${{ secrets.PROD_USER }} + PROD_SSH_KEY: ${{ secrets.PROD_SSH_KEY }} + PROD_SSH_KNOWN_HOSTS: ${{ secrets.PROD_SSH_KNOWN_HOSTS }} + PROD_PORT: ${{ secrets.PROD_PORT || '22' }} + run: | + set -euo pipefail + bash tools/ci/gitea-actions-ssh-config.sh production "$PROD_HOST" "$PROD_USER" "$PROD_PORT" "$PROD_SSH_KEY" "$PROD_SSH_KNOWN_HOSTS" + echo "=== Pre-deployment state ===" + ssh -F "${HOME}/.ssh/gitea_ci_production.conf" production "cd /opt/bitreport/ai-news-writer && pm2 list" + ssh -F "${HOME}/.ssh/gitea_ci_production.conf" production "cd /opt/bitreport/ai-news-writer && git log --oneline -1" + echo "=== Starting production deployment ===" + ssh -F "${HOME}/.ssh/gitea_ci_production.conf" production "cd /opt/bitreport/ai-news-writer && git pull --ff-only origin main" + ssh -F "${HOME}/.ssh/gitea_ci_production.conf" production "cd /opt/bitreport/ai-news-writer && npm ci --omit=dev && npm run lint --quiet" + ssh -F "${HOME}/.ssh/gitea_ci_production.conf" production "cd /opt/bitreport/ai-news-writer && pm2 startOrReload ecosystem.config.cjs --update-env" + echo "Production deployment code updates completed" + + - name: Health check — production + env: + PROD_HOST: ${{ secrets.PROD_HOST }} + PROD_USER: ${{ secrets.PROD_USER }} + PROD_SSH_KEY: ${{ secrets.PROD_SSH_KEY }} + PROD_SSH_KNOWN_HOSTS: ${{ secrets.PROD_SSH_KNOWN_HOSTS }} + PROD_PORT: ${{ secrets.PROD_PORT || '22' }} + run: | + set -euo pipefail + bash tools/ci/gitea-actions-ssh-config.sh production "$PROD_HOST" "$PROD_USER" "$PROD_PORT" "$PROD_SSH_KEY" "$PROD_SSH_KNOWN_HOSTS" + sleep 10 + echo "=== Running health checks ===" + ssh -F "${HOME}/.ssh/gitea_ci_production.conf" production "cd /opt/bitreport/ai-news-writer && bash tools/verify-deploy.sh http://127.0.0.1:3200" + echo "=== Additional validations ===" + ssh -F "${HOME}/.ssh/gitea_ci_production.conf" production "cd /opt/bitreport/ai-news-writer && npm run wp:test --silent || echo 'WP test optional'" + ssh -F "${HOME}/.ssh/gitea_ci_production.conf" production "cd /opt/bitreport/ai-news-writer && npm run rss:test --silent || echo 'RSS test optional'" + echo "Production health checks passed" + + - name: Smoke test — external endpoint + env: + PROD_URL: ${{ secrets.PROD_URL || 'https://ainews.bitreport.pt' }} + run: | + echo "=== External endpoint validation ===" + curl -s -I -H "User-Agent: Gitea-CI" "$PROD_URL/health" | head -5 + RESPONSE=$(curl -s "$PROD_URL/health") + if echo "$RESPONSE" | grep -q '"ok":true'; then + echo "External health check passed" + else + echo "External health check failed or invalid response" + exit 1 + fi + + - name: Rollback on failure + if: failure() + env: + PROD_HOST: ${{ secrets.PROD_HOST }} + PROD_USER: ${{ secrets.PROD_USER }} + PROD_SSH_KEY: ${{ secrets.PROD_SSH_KEY }} + PROD_SSH_KNOWN_HOSTS: ${{ secrets.PROD_SSH_KNOWN_HOSTS }} + PROD_PORT: ${{ secrets.PROD_PORT || '22' }} + run: | + set -euo pipefail + bash tools/ci/gitea-actions-ssh-config.sh production "$PROD_HOST" "$PROD_USER" "$PROD_PORT" "$PROD_SSH_KEY" "$PROD_SSH_KNOWN_HOSTS" + echo "Deployment failed — initiating rollback" + ssh -F "${HOME}/.ssh/gitea_ci_production.conf" production "cd /opt/bitreport/ai-news-writer && git log --oneline -3" + ssh -F "${HOME}/.ssh/gitea_ci_production.conf" production "cd /opt/bitreport/ai-news-writer && git revert HEAD --no-edit && git push origin main" + ssh -F "${HOME}/.ssh/gitea_ci_production.conf" production "cd /opt/bitreport/ai-news-writer && git pull --ff-only origin main && npm ci --omit=dev && pm2 startOrReload ecosystem.config.cjs --update-env" + echo "Production deployment failed — rollback attempted" + exit 1 + + - name: Post-deployment summary + if: success() + run: | + echo "Production deployment completed successfully" + echo "Branch: ${{ github.ref_name }}" + echo "Commit: ${{ github.sha }}" + echo "Run ID: ${{ github.run_id }}" + echo "Deployment notes: ${{ github.event.inputs.deployment_notes }}" + echo "Production URL: ${{ secrets.PROD_URL || 'https://ainews.bitreport.pt' }}" + + - name: Post-deployment failure summary + if: failure() + run: | + echo "Production deployment FAILED" + echo "Branch: ${{ github.ref_name }}" + echo "Commit: ${{ github.sha }}" + echo "Run ID: ${{ github.run_id }}" + + notify-completion: + needs: deploy-production + if: always() + runs-on: ubuntu-latest + steps: + - name: Deployment status notification + run: | + STATUS="${{ needs.deploy-production.result }}" + if [ "$STATUS" = "success" ]; then + echo "Production deployment: SUCCESS" + else + echo "Production deployment: FAILED (check workflow logs)" + fi diff --git a/.gitea/workflows/deploy-staging.yml b/.gitea/workflows/deploy-staging.yml new file mode 100644 index 0000000..415eb6a --- /dev/null +++ b/.gitea/workflows/deploy-staging.yml @@ -0,0 +1,83 @@ +# Gitea Actions — Staging deployment after CI (develop). +# +# Required org/repo secrets: +# STAGING_HOST, STAGING_USER, STAGING_SSH_KEY, STAGING_SSH_KNOWN_HOSTS +# Optional: STAGING_PORT (default 22), STAGING_URL (display only) +# +# STAGING_SSH_KNOWN_HOSTS: multiline `ssh-keyscan -p -H ` for the deploy target (pin host keys; no MITM). +name: ai-news-writer-deploy-staging + +on: + workflow_run: + workflows: ["ai-news-writer-ci"] + types: [completed] + branches: [develop] + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: false + +jobs: + deploy-staging: + if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }} + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Deploy to staging via SSH + env: + STAGING_HOST: ${{ secrets.STAGING_HOST }} + STAGING_USER: ${{ secrets.STAGING_USER }} + STAGING_SSH_KEY: ${{ secrets.STAGING_SSH_KEY }} + STAGING_SSH_KNOWN_HOSTS: ${{ secrets.STAGING_SSH_KNOWN_HOSTS }} + STAGING_PORT: ${{ secrets.STAGING_PORT || '22' }} + run: | + set -euo pipefail + bash tools/ci/gitea-actions-ssh-config.sh staging "$STAGING_HOST" "$STAGING_USER" "$STAGING_PORT" "$STAGING_SSH_KEY" "$STAGING_SSH_KNOWN_HOSTS" + ssh -F "${HOME}/.ssh/gitea_ci_staging.conf" staging "cd /opt/bitreport/ai-news-writer && git pull --ff-only origin develop && npm ci --omit=dev && npm run lint --quiet" + ssh -F "${HOME}/.ssh/gitea_ci_staging.conf" staging "cd /opt/bitreport/ai-news-writer && pm2 startOrReload ecosystem.config.cjs --update-env" + + - name: Health check — staging + env: + STAGING_HOST: ${{ secrets.STAGING_HOST }} + STAGING_USER: ${{ secrets.STAGING_USER }} + STAGING_SSH_KEY: ${{ secrets.STAGING_SSH_KEY }} + STAGING_SSH_KNOWN_HOSTS: ${{ secrets.STAGING_SSH_KNOWN_HOSTS }} + STAGING_PORT: ${{ secrets.STAGING_PORT || '22' }} + run: | + set -euo pipefail + bash tools/ci/gitea-actions-ssh-config.sh staging "$STAGING_HOST" "$STAGING_USER" "$STAGING_PORT" "$STAGING_SSH_KEY" "$STAGING_SSH_KNOWN_HOSTS" + sleep 5 + ssh -F "${HOME}/.ssh/gitea_ci_staging.conf" staging "cd /opt/bitreport/ai-news-writer && bash tools/verify-deploy.sh http://127.0.0.1:3200" + echo "Staging deployment successful — health check passed" + + - name: Automatic rollback on failure + if: failure() + env: + STAGING_HOST: ${{ secrets.STAGING_HOST }} + STAGING_USER: ${{ secrets.STAGING_USER }} + STAGING_SSH_KEY: ${{ secrets.STAGING_SSH_KEY }} + STAGING_SSH_KNOWN_HOSTS: ${{ secrets.STAGING_SSH_KNOWN_HOSTS }} + STAGING_PORT: ${{ secrets.STAGING_PORT || '22' }} + run: | + set -euo pipefail + bash tools/ci/gitea-actions-ssh-config.sh staging "$STAGING_HOST" "$STAGING_USER" "$STAGING_PORT" "$STAGING_SSH_KEY" "$STAGING_SSH_KNOWN_HOSTS" + echo "Attempting automatic rollback to previous commit..." + ssh -F "${HOME}/.ssh/gitea_ci_staging.conf" staging "cd /opt/bitreport/ai-news-writer && git log --oneline -2" + ssh -F "${HOME}/.ssh/gitea_ci_staging.conf" staging "cd /opt/bitreport/ai-news-writer && git revert HEAD --no-edit && git push origin develop" + ssh -F "${HOME}/.ssh/gitea_ci_staging.conf" staging "cd /opt/bitreport/ai-news-writer && git pull --ff-only origin develop && npm ci --omit=dev && pm2 startOrReload ecosystem.config.cjs --update-env" + echo "Staging deployment failed — rollback attempted" + exit 1 + + - name: Post deployment status + if: always() + run: | + if [ "${{ job.status }}" = "success" ]; then + echo "Staging deployment completed successfully" + echo "Environment: ${{ secrets.STAGING_URL || 'https://ainews-staging.bitreport.pt' }}" + else + echo "Staging deployment failed with status: ${{ job.status }}" + fi diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index f7a789f..5f8573b 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -58,3 +58,5 @@ jobs: run: | python3 -m pip install --disable-pip-version-check --quiet pyyaml --break-system-packages python3 -c "import yaml; yaml.safe_load(open('.gitea/workflows/test.yml'))" + python3 -c "import yaml; yaml.safe_load(open('.gitea/workflows/deploy-staging.yml'))" + python3 -c "import yaml; yaml.safe_load(open('.gitea/workflows/deploy-production.yml'))" diff --git a/tools/ci/gitea-actions-ssh-config.sh b/tools/ci/gitea-actions-ssh-config.sh new file mode 100755 index 0000000..cac2a42 --- /dev/null +++ b/tools/ci/gitea-actions-ssh-config.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash +# Write a dedicated OpenSSH client config for Gitea Actions deploy (no global ~/.ssh/config append). +# Writes ~/.ssh/gitea_ci_.conf — use: ssh -F "$HOME/.ssh/gitea_ci_staging.conf" staging "cmd" +# +# Usage: +# bash tools/ci/gitea-actions-ssh-config.sh staging "$STAGING_HOST" "$STAGING_USER" "$STAGING_PORT" "$STAGING_SSH_KEY" "$STAGING_SSH_KNOWN_HOSTS" +# ssh -F "$HOME/.ssh/gitea_ci_staging.conf" staging "remote command" +set -euo pipefail + +ALIAS="${1:?host alias (staging|production)}" +HOST="${2:?SSH host}" +USER="${3:?SSH user}" +PORT="${4:?SSH port}" +KEY_BODY="${5:?private key PEM}" +KNOWN_HOSTS="${6:-}" + +if [[ "${#KNOWN_HOSTS}" -lt 12 ]]; then + echo "error: known-hosts secret missing or too short — add multiline output of: ssh-keyscan -p PORT -H HOST" >&2 + exit 1 +fi + +mkdir -p "${HOME}/.ssh" +chmod 700 "${HOME}/.ssh" + +KEY_FILE="${HOME}/.ssh/${ALIAS}_gitea_deploy_key" +printf '%s\n' "${KEY_BODY}" >"${KEY_FILE}" +chmod 600 "${KEY_FILE}" + +KNOWN_FILE="${HOME}/.ssh/known_hosts.${ALIAS}.gitea" +printf '%s\n' "${KNOWN_HOSTS}" >"${KNOWN_FILE}" +chmod 644 "${KNOWN_FILE}" + +CONF="${HOME}/.ssh/gitea_ci_${ALIAS}.conf" +{ + printf 'Host %s\n' "${ALIAS}" + printf ' HostName %s\n' "${HOST}" + printf ' User %s\n' "${USER}" + printf ' Port %s\n' "${PORT}" + printf ' IdentityFile %s\n' "${KEY_FILE}" + printf ' UserKnownHostsFile %s\n' "${KNOWN_FILE}" + printf ' StrictHostKeyChecking yes\n' +} >"${CONF}" +chmod 600 "${CONF}" diff --git a/tools/verify-deploy.sh b/tools/verify-deploy.sh index 39cb809..a77debc 100755 --- a/tools/verify-deploy.sh +++ b/tools/verify-deploy.sh @@ -13,6 +13,7 @@ fi BASE="${BASE%/}" HEALTH_URL="${BASE}/health" +METRICS_URL="${BASE}/metrics" if ! command -v curl >/dev/null 2>&1; then echo "error: curl is required" >&2 @@ -81,6 +82,45 @@ if (d.rss_cron_timezone) { if (d.article_log_bytes != null) { console.log('article_log_bytes=' + d.article_log_bytes); } +if (d.metrics_summary && typeof d.metrics_summary === 'object') { + const m = d.metrics_summary; + console.log( + 'metrics_summary', + 'articles_generated=' + (m.articles_generated ?? '?'), + 'articles_published=' + (m.articles_published ?? '?'), + 'uptime_seconds=' + (m.uptime_seconds ?? '?') + ); +} +" + +_METRICS_JSON="$(curl -sfS --max-time 15 "${METRICS_URL}")" || { + echo "error: GET ${METRICS_URL} failed" >&2 + exit 1 +} + +echo "${_METRICS_JSON}" | node -e " +const fs = require('fs'); +const input = fs.readFileSync(0, 'utf8'); +let d; +try { + d = JSON.parse(input); +} catch (e) { + console.error('error: invalid JSON from /metrics'); + process.exit(1); +} +if (typeof d.timestamp !== 'string' || !d.timestamp) { + console.error('error: /metrics missing timestamp'); + process.exit(1); +} +if (typeof d.counters !== 'object' || d.counters === null) { + console.error('error: /metrics missing counters object'); + process.exit(1); +} +if (typeof d.gauges !== 'object' || d.gauges === null) { + console.error('error: /metrics missing gauges object'); + process.exit(1); +} +console.log('OK metrics JSON timestamp=' + d.timestamp); " echo ""