From ca8bec5ca3fed68a6dd83e31c0fbb4240df9894a Mon Sep 17 00:00:00 2001 From: Xika Date: Tue, 12 May 2026 12:05:48 +0000 Subject: [PATCH] ci(staging): deploy after CI on main; pull triggering branch (BIT-21911) Staging deploy previously chained only off develop CI and hard-coded git pull develop. Remote develop was missing or protected while PR #18 merged to main. Watch develop and main; set STAGING_GIT_BRANCH from workflow_run or dispatch ref; align rollback push and pull with the same branch. Docs: DEPLOYMENT runbook diagram and manual-dispatch notes. Co-authored-by: Cursor --- .gitea/workflows/deploy-staging.yml | 21 +++++++++++++++------ docs/runbooks/DEPLOYMENT.md | 16 ++++++++-------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/.gitea/workflows/deploy-staging.yml b/.gitea/workflows/deploy-staging.yml index cea9628..757787c 100644 --- a/.gitea/workflows/deploy-staging.yml +++ b/.gitea/workflows/deploy-staging.yml @@ -1,4 +1,9 @@ -# Gitea Actions — Staging deployment after CI (develop). +# Gitea Actions — Staging deployment after CI (develop or main). +# +# `workflow_run` fires when `ai-news-writer-ci` completes successfully on a watched +# branch. The remote staging host pulls the same branch that passed CI +# (`STAGING_GIT_BRANCH`), so merges to `main` can promote to staging even when +# `develop` is absent or protected (BIT-21911). # # Required org/repo secrets: # STAGING_HOST, STAGING_USER, STAGING_SSH_KEY, STAGING_SSH_KNOWN_HOSTS @@ -11,11 +16,11 @@ name: ai-news-writer-deploy-staging workflow_run: workflows: ["ai-news-writer-ci"] types: [completed] - branches: [develop] + branches: [develop, main] workflow_dispatch: concurrency: - group: ${{ github.workflow }}-${{ github.ref }} + group: ${{ github.workflow }}-${{ github.event.workflow_run.head_branch || github.ref_name || 'default' }} cancel-in-progress: false jobs: @@ -23,6 +28,8 @@ jobs: if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }} runs-on: ubuntu-latest timeout-minutes: 15 + env: + STAGING_GIT_BRANCH: ${{ github.event_name == 'workflow_dispatch' && github.ref_name || github.event.workflow_run.head_branch }} steps: - name: Checkout uses: actions/checkout@v4 @@ -36,8 +43,9 @@ jobs: STAGING_PORT: ${{ secrets.STAGING_PORT || '22' }} run: | set -euo pipefail + BRANCH="${STAGING_GIT_BRANCH:?STAGING_GIT_BRANCH is required}" 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 && git fetch origin && git pull --ff-only origin ${BRANCH} && 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 @@ -64,11 +72,12 @@ jobs: STAGING_PORT: ${{ secrets.STAGING_PORT || '22' }} run: | set -euo pipefail + BRANCH="${STAGING_GIT_BRANCH:?STAGING_GIT_BRANCH is required}" 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" + ssh -F "${HOME}/.ssh/gitea_ci_staging.conf" staging "cd /opt/bitreport/ai-news-writer && git revert HEAD --no-edit && git push origin ${BRANCH}" + ssh -F "${HOME}/.ssh/gitea_ci_staging.conf" staging "cd /opt/bitreport/ai-news-writer && git fetch origin && git pull --ff-only origin ${BRANCH} && npm ci --omit=dev && pm2 startOrReload ecosystem.config.cjs --update-env" echo "Staging deployment failed — rollback attempted" exit 1 diff --git a/docs/runbooks/DEPLOYMENT.md b/docs/runbooks/DEPLOYMENT.md index b65e735..48ea246 100644 --- a/docs/runbooks/DEPLOYMENT.md +++ b/docs/runbooks/DEPLOYMENT.md @@ -14,19 +14,19 @@ Related issues: BIT-14770 (this runbook), BIT-2708 (CI/ops programme). developer push │ ▼ - [develop branch] + [develop or main branch] │ ▼ ┌──────────────────────────────┐ │ Gitea CI (test.yml) │ │ lint + audit + test │ └──────────┬───────────────────┘ - │ success + │ success (same branch) ▼ ┌──────────────────────────────┐ │ Staging auto-deploy │ ← deploy-staging.yml - │ git pull + npm ci + pm2 │ - │ reload + verify-deploy.sh │ + │ git pull that branch + npm │ + │ ci + pm2 reload + verify │ └──────────┬───────────────────┘ │ passes QA on staging ▼ @@ -43,7 +43,7 @@ developer push └──────────────────────────────┘ ``` -Staging deploys automatically on every successful CI run against `develop`. +Staging deploys automatically on every successful CI run against `develop` **or** `main` (the workflow pulls the same branch that passed CI — see `.gitea/workflows/deploy-staging.yml`). Production requires an explicit **manual trigger** with the word `PROMOTE`. --- @@ -52,7 +52,7 @@ Production requires an explicit **manual trigger** with the word `PROMOTE`. | Setting | Dev (local) | Staging | Production | |---------|-------------|---------|------------| -| Branch | any | `develop` | `main` | +| Branch | any | `develop` or `main` (matches CI branch) | `main` | | Host/CT | localhost | CTID from `STAGING_HOST` secret | CTID 223 | | Port | 3200 | 3200 | 3200 | | URL | `http://localhost:3200` | `https://ainews-staging.bitreport.pt` | `https://ainews.bitreport.pt` | @@ -89,11 +89,11 @@ Monitor in Gitea: `BitReport.pt/ai-news-writer` → Actions → `ai-news-writer- ### Manual trigger 1. Go to Gitea Actions → `ai-news-writer-deploy-staging` → **Run workflow** -2. Select branch `develop` +2. Select branch `develop` or `main` (must match the branch you want on the staging host) 3. Click **Run** The pipeline: -1. `git pull --ff-only origin develop` +1. `git fetch origin && git pull --ff-only origin ` where `` is the branch that passed CI (`develop` or `main`) or the branch you chose for **Run workflow** 2. `npm ci --omit=dev && npm run lint --quiet` 3. `pm2 startOrReload ecosystem.config.cjs --update-env` 4. Waits 5 s, runs `tools/verify-deploy.sh http://127.0.0.1:3200`