|
1 | | -name: Deploy to EC2 |
| 1 | +name: CI/CD Deployment |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | push: |
|
8 | 8 | workflow_dispatch: |
9 | 9 |
|
10 | 10 | jobs: |
| 11 | + # ---------------------------- |
| 12 | + # 1️⃣ Test stage |
| 13 | + # ---------------------------- |
11 | 14 | test: |
12 | 15 | runs-on: ubuntu-latest |
13 | 16 |
|
14 | 17 | steps: |
| 18 | + # Checkout repo |
15 | 19 | - uses: actions/checkout@v4 |
16 | 20 |
|
| 21 | + # Setup Node.js |
17 | 22 | - name: Setup Node.js |
18 | 23 | uses: actions/setup-node@v4 |
19 | 24 | with: |
20 | 25 | node-version: "20" |
21 | 26 | cache: "npm" |
22 | 27 |
|
| 28 | + # Install dependencies |
23 | 29 | - name: Install dependencies |
24 | 30 | run: npm ci |
25 | 31 |
|
| 32 | + # Lint code |
26 | 33 | - name: Lint |
27 | 34 | run: npm run lint |
28 | 35 |
|
| 36 | + # Optional: add tests |
29 | 37 | # - name: Test |
30 | 38 | # run: npm test |
31 | 39 |
|
| 40 | + # ---------------------------- |
| 41 | + # 2️⃣ Deploy stage |
| 42 | + # ---------------------------- |
32 | 43 | deploy: |
33 | 44 | needs: test |
34 | 45 | runs-on: ubuntu-latest |
35 | 46 | if: github.ref == 'refs/heads/main' |
36 | 47 |
|
37 | 48 | steps: |
| 49 | + # Checkout code again |
38 | 50 | - uses: actions/checkout@v4 |
39 | 51 |
|
| 52 | + # ---------------------------- |
| 53 | + # SSH Deploy |
| 54 | + # ---------------------------- |
40 | 55 | - name: Deploy to EC2 |
41 | 56 | uses: appleboy/ssh-action@v1.0.3 |
42 | 57 | with: |
43 | 58 | host: ${{ secrets.EC2_HOST }} |
44 | 59 | username: ubuntu |
45 | 60 | key: ${{ secrets.EC2_SSH_KEY }} |
| 61 | + port: 22 |
| 62 | + timeout: 30s |
| 63 | + command_timeout: 10m |
| 64 | + debug: true |
46 | 65 | 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 |
49 | 68 | cd /var/www/CI_CD-lab |
50 | 69 |
|
51 | 70 | # Pull latest code |
52 | 71 | if [ -d ".git" ]; then |
| 72 | + git reset --hard |
| 73 | + git clean -fd |
53 | 74 | git pull origin main |
54 | 75 | else |
55 | 76 | git clone https://github.com/${{ github.repository }} . |
56 | 77 | fi |
57 | 78 |
|
58 | | - # Install dependencies |
59 | | - npm install --omit=dev |
| 79 | + # Install dependencies deterministically |
| 80 | + npm ci --omit=dev |
60 | 81 |
|
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 |
64 | 84 |
|
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 |
67 | 89 | pm2 save |
68 | 90 |
|
69 | | - # Verify app is running |
70 | | - pm2 status |
| 91 | + # Wait a few seconds then check health |
71 | 92 | 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