ci(ai-news-writer): harden Gitea deploy SSH with pinned host keys (BIT-3601)
ai-news-writer-ci / test (pull_request) Failing after 43s
ai-news-writer-ci / test (pull_request) Failing after 43s
Add tools/ci/gitea-actions-ssh-config.sh and require STAGING_SSH_KNOWN_HOSTS / PROD_SSH_KNOWN_HOSTS secrets (ssh-keyscan output). Deploy jobs use ssh -F with StrictHostKeyChecking yes instead of disabling host key verification. CI parses deploy workflow YAML alongside test.yml. Paperclip-Issue: BIT-3601 Paperclip-Run: 10d053b5-5cec-45ff-ba5f-29bb21da30d3
This commit is contained in:
1 parent
461887840a
commit
0d2d3c01a4
4 files changed
+283
No files matched your search
@@ -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 <port> -H <host>` 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
|
||||
@@ -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 <port> -H <host>` 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
|
||||
@@ -57,3 +57,5 @@ jobs:
|
||||
run: |
|
||||
python3 -m pip install --disable-pip-version-check --quiet pyyaml
|
||||
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'))"
|
||||
Executable
+43
@@ -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_<alias>.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}"
|
||||
Reference in new issue
Block a user