96 lines
2.6 KiB
YAML
96 lines
2.6 KiB
YAML
name: CI Deploy
|
||
|
||
on:
|
||
push:
|
||
branches:
|
||
- main
|
||
|
||
env:
|
||
NODE_VERSION: 20
|
||
|
||
jobs:
|
||
build-test-deploy:
|
||
runs-on: ubuntu-latest
|
||
steps:
|
||
- name: Checkout
|
||
uses: actions/checkout@v4
|
||
|
||
- name: Setup Node.js
|
||
uses: actions/setup-node@v4
|
||
with:
|
||
node-version: ${{ env.NODE_VERSION }}
|
||
cache: npm
|
||
|
||
- name: Install dependencies
|
||
run: npm ci
|
||
|
||
- name: Lint
|
||
run: npm run lint
|
||
|
||
- name: Test
|
||
run: npm run test
|
||
|
||
- name: Build
|
||
run: npm run build
|
||
|
||
- name: Prune dev dependencies
|
||
run: npm prune --omit=dev
|
||
|
||
- name: Package artifact
|
||
run: |
|
||
tar -czf qun-monitor-release.tar.gz \
|
||
dist \
|
||
keywords.yaml \
|
||
package.json \
|
||
package-lock.json \
|
||
node_modules \
|
||
.env.example \
|
||
README.md
|
||
|
||
- name: Upload artifact to server
|
||
uses: appleboy/scp-action@v0.1.7
|
||
with:
|
||
host: ${{ secrets.DEPLOY_HOST }}
|
||
username: ${{ secrets.DEPLOY_USER }}
|
||
password: ${{ secrets.DEPLOY_PASSWORD }}
|
||
port: ${{ secrets.DEPLOY_PORT || '22' }}
|
||
source: qun-monitor-release.tar.gz
|
||
target: ${{ secrets.DEPLOY_PACKAGE_DIR || '/home/atai/deployments/qun-monitor' }}
|
||
|
||
- name: Remote deploy
|
||
uses: appleboy/ssh-action@v0.1.10
|
||
with:
|
||
host: ${{ secrets.DEPLOY_HOST }}
|
||
username: ${{ secrets.DEPLOY_USER }}
|
||
password: ${{ secrets.DEPLOY_PASSWORD }}
|
||
port: ${{ secrets.DEPLOY_PORT || '22' }}
|
||
script_stop: true
|
||
command_timeout: 30m
|
||
script: |
|
||
set -euo pipefail
|
||
APP_DIR=${{ secrets.DEPLOY_APP_DIR || '/home/atai/services/qun-monitor' }}
|
||
PACKAGE_DIR=${{ secrets.DEPLOY_PACKAGE_DIR || '/home/atai/deployments/qun-monitor' }}
|
||
PACKAGE_FILE="${PACKAGE_DIR}/qun-monitor-release.tar.gz"
|
||
|
||
mkdir -p "${APP_DIR}" "${PACKAGE_DIR}"
|
||
tar -xzf "${PACKAGE_FILE}" -C "${APP_DIR}"
|
||
|
||
cd "${APP_DIR}"
|
||
|
||
if [ ! -f .env ]; then
|
||
echo "[deploy] ⚠️ 未检测到 .env,已跳过启动,请参考 .env.example 配置后自行重启。"
|
||
exit 0
|
||
fi
|
||
|
||
if ! command -v pm2 >/dev/null 2>&1; then
|
||
sudo npm install -g pm2
|
||
fi
|
||
|
||
if pm2 describe qun-monitor >/dev/null 2>&1; then
|
||
pm2 restart qun-monitor --update-env
|
||
else
|
||
pm2 start npm --name qun-monitor -- start
|
||
fi
|
||
|
||
pm2 save >/dev/null 2>&1 || true
|