Skip to content

Commit 43358c7

Browse files
committed
fix(ci): update deployment workflow and environment setup
- Load NVM in the deployment workflow for Node.js version management - Simplify backup logic for current dist and node_modules directories - Ensure .env file restoration from recent backup if missing - Use npm ci for safer production dependency installation and streamline PM2 management
1 parent 118a62f commit 43358c7

File tree

2 files changed

+28
-31
lines changed

2 files changed

+28
-31
lines changed

.github/workflows/backend.yml

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -60,52 +60,48 @@ jobs:
6060
key: ${{ secrets.VPS_SSH_KEY }}
6161
port: ${{ secrets.VPS_SSH_PORT || 22 }}
6262
script: |
63+
# ---- Load NVM (CRITICAL) ----
64+
export NVM_DIR="$HOME/.nvm"
65+
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
66+
6367
# Set deployment directory
6468
DEPLOY_DIR="${{ secrets.VPS_DEPLOY_PATH || '/root/copy-paste.space/backend' }}"
65-
66-
# Navigate to deployment directory
6769
cd "$DEPLOY_DIR"
68-
69-
# Backup current dist and node_modules if they exist
70+
71+
# Backup current dist and node_modules
7072
if [ -d "dist" ] || [ -d "node_modules" ]; then
7173
BACKUP_DIR="backup-$(date +%Y%m%d-%H%M%S)"
7274
mkdir -p "../backups/$BACKUP_DIR"
7375
[ -d "dist" ] && mv dist "../backups/$BACKUP_DIR/"
7476
[ -d "node_modules" ] && mv node_modules "../backups/$BACKUP_DIR/"
7577
fi
76-
78+
7779
# Extract new deployment
7880
tar -xzf /tmp/backend-deploy.tar.gz -C "$DEPLOY_DIR"
79-
80-
# Ensure .env file exists (copy from backup if needed)
81+
82+
# Restore .env if missing
8183
if [ ! -f ".env" ] && [ -d "../backups" ]; then
8284
LATEST_BACKUP=$(ls -td ../backups/backup-* 2>/dev/null | head -1)
83-
if [ -n "$LATEST_BACKUP" ] && [ -f "$LATEST_BACKUP/.env" ]; then
84-
cp "$LATEST_BACKUP/.env" .env
85-
fi
85+
[ -f "$LATEST_BACKUP/.env" ] && cp "$LATEST_BACKUP/.env" .env
8686
fi
87-
88-
# Install production dependencies
87+
88+
# Install production deps (safe even if node_modules exists)
8989
npm ci --production || npm install --production
90-
91-
# Restart PM2 service
92-
if /usr/local/bin/pm2 describe cp-backend-production > /dev/null 2>&1; then
93-
# Process exists, reload it (zero-downtime restart)
94-
/usr/local/bin/pm2 reload cp-backend-production --update-env
90+
91+
# Restart PM2
92+
if pm2 describe cp-backend-production > /dev/null 2>&1; then
93+
pm2 reload cp-backend-production --update-env
9594
else
96-
# Process doesn't exist, start it
97-
/usr/local/bin/pm2 start dist/server.js \
95+
pm2 start dist/server.js \
9896
--name cp-backend-production \
9997
--cwd "$DEPLOY_DIR" \
10098
--update-env
101-
/usr/local/bin/pm2 save
99+
pm2 save
102100
fi
103-
104-
# Cleanup old backups (keep last 5)
105-
if [ -d "../backups" ]; then
106-
ls -td ../backups/backup-* 2>/dev/null | tail -n +6 | xargs rm -rf
107-
fi
108-
101+
102+
# Cleanup backups (keep last 5)
103+
ls -td ../backups/backup-* 2>/dev/null | tail -n +6 | xargs rm -rf || true
104+
109105
# Cleanup temp file
110106
rm -f /tmp/backend-deploy.tar.gz
111107
@@ -117,8 +113,9 @@ jobs:
117113
key: ${{ secrets.VPS_SSH_KEY }}
118114
port: ${{ secrets.VPS_SSH_PORT || 22 }}
119115
script: |
120-
sleep 5
121-
# Check PM2 process status
122-
/usr/local/bin/pm2 status cp-backend-production
123-
/usr/local/bin/pm2 info cp-backend-production
116+
export NVM_DIR="$HOME/.nvm"
117+
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
124118
119+
sleep 5
120+
pm2 status cp-backend-production
121+
pm2 info cp-backend-production

backend/src/s3-cleanup-cron.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async function deleteExpiredTempObjects(): Promise<void> {
1414
}
1515

1616
const now = Date.now();
17-
const expirationMs = 5 * 60 * 1000; // 5 minutes
17+
const expirationMs = 5 * 60 * 1000; // 5 minutes of time
1818

1919
try {
2020
let continuationToken: string | undefined = undefined;

0 commit comments

Comments
 (0)