Skip to content

Commit b90f7ea

Browse files
authored
Refactor CI/CD deployment workflow for EC2
Updated CI/CD workflow for deployment to EC2, including improved steps for code checkout, dependency installation, and PM2 process management.
1 parent 09b3da8 commit b90f7ea

1 file changed

Lines changed: 34 additions & 13 deletions

File tree

.github/workflows/Deploy.yml

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Deploy to EC2
1+
name: CI/CD Deployment
22

33
on:
44
push:
@@ -8,65 +8,86 @@ on:
88
workflow_dispatch:
99

1010
jobs:
11+
# ----------------------------
12+
# 1️⃣ Test stage
13+
# ----------------------------
1114
test:
1215
runs-on: ubuntu-latest
1316

1417
steps:
18+
# Checkout repo
1519
- uses: actions/checkout@v4
1620

21+
# Setup Node.js
1722
- name: Setup Node.js
1823
uses: actions/setup-node@v4
1924
with:
2025
node-version: "20"
2126
cache: "npm"
2227

28+
# Install dependencies
2329
- name: Install dependencies
2430
run: npm ci
2531

32+
# Lint code
2633
- name: Lint
2734
run: npm run lint
2835

36+
# Optional: add tests
2937
# - name: Test
3038
# run: npm test
3139

40+
# ----------------------------
41+
# 2️⃣ Deploy stage
42+
# ----------------------------
3243
deploy:
3344
needs: test
3445
runs-on: ubuntu-latest
3546
if: github.ref == 'refs/heads/main'
3647

3748
steps:
49+
# Checkout code again
3850
- uses: actions/checkout@v4
3951

52+
# ----------------------------
53+
# SSH Deploy
54+
# ----------------------------
4055
- name: Deploy to EC2
4156
uses: appleboy/ssh-action@v1.0.3
4257
with:
4358
host: ${{ secrets.EC2_HOST }}
4459
username: ubuntu
4560
key: ${{ secrets.EC2_SSH_KEY }}
61+
port: 22
62+
timeout: 30s
63+
command_timeout: 10m
64+
debug: true
4665
script: |
47-
cd /var/www/CI_CD-lab || sudo mkdir -p /var/www/CI_CD-lab
48-
sudo chown ubuntu:ubuntu /var/www/CI_CD-lab
66+
# Ensure deployment folder exists
67+
mkdir -p /var/www/CI_CD-lab
4968
cd /var/www/CI_CD-lab
5069
5170
# Pull latest code
5271
if [ -d ".git" ]; then
72+
git reset --hard
73+
git clean -fd
5374
git pull origin main
5475
else
5576
git clone https://github.com/${{ github.repository }} .
5677
fi
5778
58-
# Install dependencies
59-
npm install --omit=dev
79+
# Install dependencies deterministically
80+
npm ci --omit=dev
6081
61-
# Stop existing app (gracefully handle if not running)
62-
pm2 stop ci-cd-github 2>/dev/null || echo "App not running, starting fresh"
63-
pm2 delete ci-cd-github 2>/dev/null || echo "No existing app to delete"
82+
# Ensure PM2 installed
83+
pm2 -v || npm install -g pm2
6484
65-
# Start app
66-
pm2 start app.js --name ci-cd-github
85+
# Zero-downtime reload
86+
pm2 reload ci-cd-github || pm2 start app.js --name ci-cd-github
87+
88+
# Save PM2 process list
6789
pm2 save
6890
69-
# Verify app is running
70-
pm2 status
91+
# Wait a few seconds then check health
7192
sleep 5
72-
curl -f http://localhost:3000/health || echo "Health check warning"
93+
curl -f http://localhost:3000/health || exit 1

0 commit comments

Comments
 (0)