ai-news-writer-ci / test (pull_request) Successful in 33s
Staging CT had diverged local commits from a previous rollback that pushed a
revert commit to the remote. git pull --ff-only was failing with
"Not possible to fast-forward". Replace with git reset --hard origin/<branch>
so staging always matches remote state regardless of local history.
Rollback now uses git reset --hard HEAD@{1} locally instead of reverting and
pushing to origin — staging rollback has no business touching the remote repo.
Paperclip-Issue: BIT-22325
Paperclip-Agent: tiago-neves
92 lines
4.5 KiB
YAML
92 lines
4.5 KiB
YAML
# 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
|
|
# 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, main]
|
|
workflow_dispatch:
|
|
|
|
concurrency:
|
|
group: ${{ github.workflow }}-${{ github.event.workflow_run.head_branch || github.ref_name || 'default' }}
|
|
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
|
|
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
|
|
|
|
- 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
|
|
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 fetch origin && git checkout ${BRANCH} && git reset --hard origin/${BRANCH} && npm ci --omit=dev"
|
|
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
|
|
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 -3"
|
|
ssh -F "${HOME}/.ssh/gitea_ci_staging.conf" staging "cd /opt/bitreport/ai-news-writer && git reset --hard HEAD@{1} && 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
|