Skip to content

Commit 35595fc

Browse files
committed
chore(ci,c d): fix health-check, release packaging, PM2 symlink deploy; update Node/actions; clean comments; update server setup
1 parent 157820a commit 35595fc

6 files changed

Lines changed: 25 additions & 24 deletions

File tree

.github/workflows/ci.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ jobs:
1313

1414
steps:
1515
- name: Checkout code
16-
uses: actions/checkout@v3
16+
uses: actions/checkout@v4
1717

1818
- name: Set up Node.js
19-
uses: actions/setup-node@v3
19+
uses: actions/setup-node@v4
2020
with:
21-
node-version: "18"
21+
node-version: "20"
22+
cache: npm
23+
cache-dependency-path: package-lock.json
2224

2325
- name: Install dependencies
2426
run: npm ci
@@ -34,14 +36,14 @@ jobs:
3436
contents: write
3537
steps:
3638
- name: Checkout code
37-
uses: actions/checkout@v3
39+
uses: actions/checkout@v4
3840
with:
3941
fetch-depth: "0"
4042

4143
- name: Bump version and push tag
4244
uses: anothrNick/github-tag-action@v1
4345
env:
44-
GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
46+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4547
DEFAULT_BUMP: patch
4648
TAG_PREFIX: v
4749
WITH_V: true # Ensures consistent v prefix

.github/workflows/health-check.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
id: check_prod
1414
run: |
1515
set +e # Allow curl to fail without exiting the script
16-
CURL_OUTPUT=$(curl -s -o /dev/null -w "%{{http_code}}" ${{ secrets.PROD_URL }}/health 2>&1)
16+
CURL_OUTPUT=$(curl -s -o /dev/null -w "%{http_code}" ${{ secrets.PROD_URL }}/api/health 2>&1)
1717
HTTP_CODE=$(echo $CURL_OUTPUT | tail -n 1)
1818
CURL_ERROR=$(echo $CURL_OUTPUT | head -n -1)
1919
@@ -29,7 +29,7 @@ jobs:
2929
id: check_dev
3030
run: |
3131
set +e # Allow curl to fail without exiting the script
32-
CURL_OUTPUT=$(curl -s -o /dev/null -w "%{{http_code}}" ${{ secrets.DEV_URL }}/health 2>&1)
32+
CURL_OUTPUT=$(curl -s -o /dev/null -w "%{http_code}" ${{ secrets.DEV_URL }}/api/health 2>&1)
3333
HTTP_CODE=$(echo $CURL_OUTPUT | tail -n 1)
3434
CURL_ERROR=$(echo $CURL_OUTPUT | head -n -1)
3535

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
run: |
2828
mkdir deploy_package
2929
# include your deploy.sh and any other scripts here
30-
cp -r src public scripts package.json package-lock.json .env.example deploy_package/
30+
cp -r src public scripts package.json package-lock.json deploy_package/
3131
tar -czf deployment.tar.gz -C deploy_package .
3232
3333
- name: Deploy to EC2

scripts/deploy.sh

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ echo "🚀 Starting deployment process..."
1515
# Create necessary directories
1616
sudo mkdir -p $RELEASES_DIR
1717
sudo mkdir -p $BACKUP_DIR
18-
sudo chown -R ubuntu:ubuntu $APP_DIR
19-
sudo chown -R ubuntu:ubuntu $BACKUP_DIR
18+
sudo chown -R ${USER}:${USER} $APP_DIR
19+
sudo chown -R ${USER}:${USER} $BACKUP_DIR
2020

2121
# Create new release directory
2222
mkdir -p $NEW_RELEASE_DIR
@@ -31,23 +31,24 @@ tar -xzf /tmp/deployment.tar.gz -C $NEW_RELEASE_DIR
3131
echo "📋 Installing dependencies..."
3232
# Install only production dependencies
3333
cd $NEW_RELEASE_DIR
34-
npm ci --only=production
34+
npm ci --omit=dev
3535

3636
echo "🔄 Managing application process..."
3737

38-
# Check if the service is already running
38+
# Symlink the new release to current BEFORE managing PM2
39+
ln -nfs $NEW_RELEASE_DIR $CURRENT_RELEASE_DIR
40+
41+
# Manage PM2 with stable path pointing at current symlink
3942
if pm2 list | grep -q $SERVICE_NAME; then
4043
echo "🔄 Reloading application with PM2 for zero-downtime deployment..."
41-
pm2 reload $SERVICE_NAME
44+
pm2 reload $SERVICE_NAME --update-env
4245
else
4346
echo "🚀 Starting application with PM2..."
44-
# Set production environment
4547
export NODE_ENV=production
4648
export PORT=3000
4749
export HOST=0.0.0.0
4850
export DEPLOYMENT_TIME=$(date -u +"%Y-%m-%dT%H:%M:%S.000Z")
49-
50-
pm2 start $NEW_RELEASE_DIR/src/server.js --name $SERVICE_NAME --env production
51+
pm2 start $CURRENT_RELEASE_DIR/src/server.js --name $SERVICE_NAME --env production
5152
fi
5253

5354
# Create a backup of the previous release
@@ -60,14 +61,13 @@ if [ -L "$CURRENT_RELEASE_DIR" ]; then
6061
fi
6162
fi
6263

63-
# Symlink the new release to current
64-
ln -nfs $NEW_RELEASE_DIR $CURRENT_RELEASE_DIR
64+
# Symlink already updated above
6565

6666
# Save PM2 configuration
6767
pm2 save
6868

6969
# Setup PM2 to start on system boot
70-
pm2 startup ubuntu -u ubuntu --hp /home/ubuntu || echo "PM2 startup already configured"
70+
pm2 startup || echo "PM2 startup already configured"
7171

7272
echo "🔍 Performing health check..."
7373
# Wait for application to start

server_config

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ ssh -i github-actions-key.pem ubuntu@YOUR-EC2-PUBLIC-IP
44
# Update system packages
55
sudo apt update && sudo apt upgrade -y
66

7-
# Install Node.js 18.x
8-
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
7+
# Install Node.js 20.x (LTS)
8+
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
99
sudo apt-get install -y nodejs
1010

1111
# Verify installation
@@ -20,8 +20,8 @@ sudo apt install nginx -y
2020

2121
# Create application directory
2222
sudo mkdir -p /var/www/app
23-
sudo chown -R ubuntu:ubuntu /var/www/app
23+
sudo chown -R $USER:$USER /var/www/app
2424

2525
# Create backup directory
2626
sudo mkdir -p /var/www/backup
27-
sudo chown -R ubuntu:ubuntu /var/www/backup
27+
sudo chown -R $USER:$USER /var/www/backup

src/app.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ app.use(express.static(path.join(__dirname, '../public')));
2424
app.use('/api', apiRoutes);
2525
app.use('/api/health', healthRoutes);
2626

27-
// 404 handler
2827
// 404 handler
2928
app.use((req, res) => {
3029
res.status(404).json({ error: 'Route not found' });

0 commit comments

Comments
 (0)