From 18157ad2a01105692567f012d26518582b4919bc Mon Sep 17 00:00:00 2001 From: itzMiney Date: Mon, 9 Nov 2026 01:02:32 +0100 Subject: [PATCH] add health check route and deploy workflow --- .github/workflows/deploy.yml | 141 +++++++++++++++++++++++++++++++++++ src/cdn.routes.ts | 4 + 2 files changed, 145 insertions(+) create mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..32a5aaa --- /dev/null +++ b/.github/workflows/deploy.yml @@ -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 \ No newline at end of file diff --git a/src/cdn.routes.ts b/src/cdn.routes.ts index 4207fa9..65b7635 100644 --- a/src/cdn.routes.ts +++ b/src/cdn.routes.ts @@ -3,6 +3,10 @@ import {CdnResolver} from "./services/resolver.service"; const router = Router(); +router.get('/health', (req, res) => { + res.status(200).json({ status: 'ok', timestamp: new Date().toISOString() }); +}); + router.get('/{*splat}', (req, res) => { return CdnResolver.handleRequest(req.path, req.query, res); });