add health check route and deploy workflow
This commit is contained in:
1 parent
7e7b6d37f1
commit
18157ad2a0
2 files changed
+145
No files matched your search
@@ -0,0 +1,141 @@
|
||||
name: CDN Resolver CI/CD
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
paths-ignore:
|
||||
- '.gitignore'
|
||||
- '**.md'
|
||||
- 'LICENSE'
|
||||
- '.gitea/workflows/**'
|
||||
- '.github/workflows/**'
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
paths-ignore:
|
||||
- '.gitignore'
|
||||
- '**.md'
|
||||
- 'LICENSE'
|
||||
- '.gitea/workflows/**'
|
||||
- '.github/workflows/**'
|
||||
workflow_dispatch:
|
||||
|
||||
env:
|
||||
RELEASES_DIR: /srv/cdn-resolver/releases
|
||||
CURRENT_LINK: /srv/cdn-resolver/current
|
||||
PORT: '6020'
|
||||
NODE_ENV: 'production'
|
||||
|
||||
jobs:
|
||||
test-and-build:
|
||||
runs-on: self-hosted
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '24'
|
||||
|
||||
- name: Show versions
|
||||
run: |
|
||||
set -eux
|
||||
node -v
|
||||
npm -v
|
||||
|
||||
- name: Install dependencies and build
|
||||
run: |
|
||||
set -eux
|
||||
npm ci
|
||||
npm run build
|
||||
|
||||
- name: Type check verification
|
||||
run: |
|
||||
set -eux
|
||||
npm run check
|
||||
|
||||
deploy:
|
||||
needs: test-and-build
|
||||
runs-on: self-hosted
|
||||
if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
|
||||
env:
|
||||
SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '24'
|
||||
|
||||
- name: Install dependencies and compile
|
||||
run: |
|
||||
set -eux
|
||||
npm ci
|
||||
npm run build
|
||||
|
||||
- name: Deploy release
|
||||
run: |
|
||||
set -eux
|
||||
|
||||
mkdir -p "$HOME/.ssh"
|
||||
echo "$SSH_PRIVATE_KEY" | tr -d '\r' > "$HOME/.ssh/deploy_key"
|
||||
chmod 600 "$HOME/.ssh/deploy_key"
|
||||
|
||||
SSH_ARGS="-i $HOME/.ssh/deploy_key -o StrictHostKeyChecking=no"
|
||||
RELEASE_ID="cdn-$(date -u +%Y%m%d-%H%M%S)"
|
||||
TARGET_DIR="$RELEASES_DIR/$RELEASE_ID"
|
||||
|
||||
ssh $SSH_ARGS cdn-resolver@localhost "mkdir -p $TARGET_DIR"
|
||||
|
||||
# Sync built dist folder and package files over to the release directory
|
||||
rsync -az --delete \
|
||||
--exclude='.git' \
|
||||
--exclude='.gitea' \
|
||||
--exclude='.github' \
|
||||
--exclude='src' \
|
||||
--exclude='node_modules' \
|
||||
--exclude='.env' \
|
||||
-e "ssh $SSH_ARGS" \
|
||||
./ cdn-resolver@localhost:"$TARGET_DIR/"
|
||||
|
||||
ssh $SSH_ARGS cdn-resolver@localhost "cd $TARGET_DIR && npm ci --omit=dev"
|
||||
ssh $SSH_ARGS cdn-resolver@localhost "ln -sfn $TARGET_DIR $CURRENT_LINK"
|
||||
|
||||
ssh $SSH_ARGS cdn-resolver@localhost "sudo systemctl restart cdn-resolver.service"
|
||||
|
||||
# Health verification
|
||||
for i in $(seq 1 15); do
|
||||
if ssh $SSH_ARGS cdn-resolver@localhost "curl -fsS http://127.0.0.1:${PORT}/health" > /dev/null; then
|
||||
echo "Live health OK"
|
||||
exit 0
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
echo "Live health check failed after deploy"
|
||||
exit 1
|
||||
|
||||
- name: Cleanup old releases
|
||||
run: |
|
||||
set -eux
|
||||
mkdir -p "$HOME/.ssh"
|
||||
echo "$SSH_PRIVATE_KEY" | tr -d '\r' > "$HOME/.ssh/deploy_key"
|
||||
chmod 600 "$HOME/.ssh/deploy_key"
|
||||
|
||||
ssh -i "$HOME/.ssh/deploy_key" -o StrictHostKeyChecking=no cdn-resolver@localhost << 'EOF'
|
||||
set -eux
|
||||
RELEASES_DIR="/srv/cdn-resolver/releases"
|
||||
|
||||
old_releases="$(find "$RELEASES_DIR" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' \
|
||||
| grep -E '^cdn-[0-9]{8}-[0-9]{6}$' || true)"
|
||||
|
||||
if [ -n "$old_releases" ]; then
|
||||
printf '%s\n' "$old_releases" \
|
||||
| sort \
|
||||
| head -n -5 \
|
||||
| while read -r old_release; do
|
||||
rm -rf "$RELEASES_DIR/$old_release"
|
||||
done
|
||||
fi
|
||||
EOF
|
||||
Reference in new issue
Block a user