agent-orchestrator/.github/workflows/deploy-vps.yml

94 lines
3.6 KiB
YAML

name: Deploy to VPS
on:
workflow_dispatch:
inputs:
deploy_sha:
description: Optional exact commit SHA to deploy. Defaults to the head of the dispatched ref.
required: false
type: string
workflow_run:
workflows: [CI]
types: [completed]
branches: [main]
concurrency:
group: deploy-vps
cancel-in-progress: false
jobs:
deploy:
if: >-
github.event_name == 'workflow_dispatch' ||
(
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.event == 'push' &&
github.event.workflow_run.head_branch == 'main'
)
runs-on: ubuntu-latest
steps:
- name: Resolve deploy target
id: resolve_deploy
env:
GH_TOKEN: ${{ github.token }}
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
DEPLOY_SHA="${{ inputs.deploy_sha }}"
if [ -z "$DEPLOY_SHA" ]; then
DEPLOY_SHA="${GITHUB_SHA}"
fi
echo "Manual dispatch: deploying SHA $DEPLOY_SHA from ref ${GITHUB_REF_NAME}."
echo "deploy_sha=$DEPLOY_SHA" >> "$GITHUB_OUTPUT"
echo "fetch_ref=${GITHUB_REF_NAME}" >> "$GITHUB_OUTPUT"
echo "should_deploy=true" >> "$GITHUB_OUTPUT"
exit 0
fi
DEPLOY_SHA="${{ github.event.workflow_run.head_sha }}"
MAIN_SHA=$(gh api repos/${{ github.repository }}/git/ref/heads/main --jq '.object.sha')
if [ "$DEPLOY_SHA" != "$MAIN_SHA" ]; then
echo "Skipping: CI SHA $DEPLOY_SHA is not the current tip of main ($MAIN_SHA). Likely a stale rerun."
echo "deploy_sha=$DEPLOY_SHA" >> "$GITHUB_OUTPUT"
echo "fetch_ref=main" >> "$GITHUB_OUTPUT"
echo "should_deploy=false" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "SHA verified: $DEPLOY_SHA is the current tip of main."
echo "deploy_sha=$DEPLOY_SHA" >> "$GITHUB_OUTPUT"
echo "fetch_ref=main" >> "$GITHUB_OUTPUT"
echo "should_deploy=true" >> "$GITHUB_OUTPUT"
- name: Deploy via SSH
if: steps.resolve_deploy.outputs.should_deploy == 'true'
uses: appleboy/ssh-action@v1.2.2
with:
host: ${{ secrets.VPS_HOST }}
username: root
key: ${{ secrets.VPS_SSH_KEY }}
# Optional: set VPS_HOST_FINGERPRINT to enforce host key pinning.
fingerprint: ${{ secrets.VPS_HOST_FINGERPRINT }}
script: |
set -e
DEPLOY_SHA="${{ steps.resolve_deploy.outputs.deploy_sha }}"
FETCH_REF="${{ steps.resolve_deploy.outputs.fetch_ref }}"
cd /root/agent-orchestrator
echo "==> Ensuring full worktree..."
if [ "$(git config --bool core.sparseCheckout || echo false)" = "true" ]; then
git sparse-checkout disable
fi
echo "==> Fetching latest changes for $FETCH_REF..."
git fetch origin "$FETCH_REF"
echo "==> Current: $(git rev-parse --short HEAD)"
echo "==> Deploying target SHA: ${DEPLOY_SHA:0:7}"
git checkout --force "$DEPLOY_SHA"
echo "==> Updated: $(git rev-parse --short HEAD)"
test -f packages/web/server/start-all.ts
echo "==> Installing dependencies..."
pnpm install --frozen-lockfile
echo "==> Building..."
pnpm build
echo "==> Restarting services..."
pm2 restart ao --update-env
pm2 restart openclaw-gateway --update-env || true
echo "==> Deploy complete: $(git rev-parse --short HEAD)"