Skip to content

Commit 09b3da8

Browse files
authored
Update EC2 deployment workflow in Deploy.yml
1 parent dfad5b1 commit 09b3da8

1 file changed

Lines changed: 22 additions & 66 deletions

File tree

.github/workflows/Deploy.yml

Lines changed: 22 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ jobs:
2626
- name: Lint
2727
run: npm run lint
2828

29+
# - name: Test
30+
# run: npm test
31+
2932
deploy:
3033
needs: test
3134
runs-on: ubuntu-latest
@@ -34,83 +37,36 @@ jobs:
3437
steps:
3538
- uses: actions/checkout@v4
3639

37-
# Debug step: verify secret is present (prints only length)
38-
- name: Debug EC2_SSH_KEY presence
39-
run: |
40-
if [ -z "${{ secrets.EC2_SSH_KEY }}" ]; then
41-
echo "EC2_SSH_KEY is empty"
42-
exit 1
43-
else
44-
LEN=$(echo -n "${{ secrets.EC2_SSH_KEY }}" | wc -c)
45-
echo "EC2_SSH_KEY present (length: ${LEN} bytes)"
46-
fi
47-
48-
# Option A: appleboy/ssh-action (original) - uncomment to use
49-
# - name: Deploy to EC2 (appleboy/ssh-action)
50-
# uses: appleboy/ssh-action@v1.0.3
51-
# with:
52-
# host: ${{ secrets.EC2_HOST }}
53-
# username: ubuntu
54-
# key: ${{ secrets.EC2_SSH_KEY }}
55-
# # If your key has a passphrase also set:
56-
# # passphrase: ${{ secrets.EC2_SSH_PASSPHRASE }}
57-
# port: 22
58-
# script: |
59-
# cd /var/www/CI_CD-lab
60-
#
61-
# # Pull latest code
62-
# if [ -d ".git" ]; then
63-
# git pull origin main
64-
# else
65-
# git clone https://github.com/${{ github.repository }} .
66-
# fi
67-
#
68-
# # Install dependencies
69-
# npm install --omit=dev
70-
#
71-
# # Stop existing app (gracefully handle if not running)
72-
# pm2 stop CI_CD-githubactions 2>/dev/null || echo "App not running, starting fresh"
73-
# pm2 delete CI_CD-githubactions 2>/dev/null || echo "No existing app to delete"
74-
#
75-
# # Start app
76-
# pm2 start app.js --name CI_CD-githubactions
77-
# pm2 save
78-
#
79-
# # Verify app is running
80-
# pm2 status
81-
# sleep 5
82-
# curl -f http://localhost:3000/health || echo "Health check warning"
83-
84-
# Option B (recommended): use ssh-agent and run ssh commands yourself
85-
- name: Start ssh-agent and add key
86-
uses: webfactory/ssh-agent@v0.8.1
40+
- name: Deploy to EC2
41+
uses: appleboy/ssh-action@v1.0.3
8742
with:
88-
ssh-private-key: ${{ secrets.EC2_SSH_KEY }}
89-
90-
- name: Deploy via ssh
91-
env:
92-
HOST: ${{ secrets.EC2_HOST }}
93-
run: |
94-
set -euo pipefail
95-
ssh -o StrictHostKeyChecking=no -p 22 ubuntu@"${HOST}" bash -s <<'SSH_CMDS'
96-
set -e
97-
cd /var/www/CI_CD-lab || exit 1
98-
43+
host: ${{ secrets.EC2_HOST }}
44+
username: ubuntu
45+
key: ${{ secrets.EC2_SSH_KEY }}
46+
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
49+
cd /var/www/CI_CD-lab
50+
51+
# Pull latest code
9952
if [ -d ".git" ]; then
10053
git pull origin main
10154
else
102-
git clone https://github.com/${GITHUB_REPOSITORY} .
55+
git clone https://github.com/${{ github.repository }} .
10356
fi
10457
58+
# Install dependencies
10559
npm install --omit=dev
10660
107-
pm2 stop CI_CD-githubactions 2>/dev/null || echo "App not running, starting fresh"
108-
pm2 delete CI_CD-githubactions 2>/dev/null || echo "No existing app to delete"
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"
10964
110-
pm2 start app.js --name CI_CD-githubactions
65+
# Start app
66+
pm2 start app.js --name ci-cd-github
11167
pm2 save
11268
69+
# Verify app is running
11370
pm2 status
11471
sleep 5
11572
curl -f http://localhost:3000/health || echo "Health check warning"
116-
SSH_CMDS

0 commit comments

Comments
 (0)