|
1 | | -name: Deploy to Testing |
| 1 | +name: Deploy to Testing Environment |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | pull_request: |
5 | | - branches: |
6 | | - - main |
7 | | - workflow_dispatch: # Manual trigger button |
| 5 | + branches: [ main ] |
| 6 | + workflow_dispatch: # Allows manual trigger button |
8 | 7 |
|
9 | 8 | jobs: |
10 | | - build-test-deploy: |
| 9 | + build-and-test: |
11 | 10 | runs-on: ubuntu-latest |
12 | | - |
| 11 | + |
13 | 12 | steps: |
14 | | - - name: Checkout code |
15 | | - uses: actions/checkout@v3 |
16 | | - |
17 | | - - name: Setup Node.js |
18 | | - uses: actions/setup-node@v3 |
19 | | - with: |
20 | | - node-version: 20 |
21 | | - |
22 | | - - name: Install dependencies |
23 | | - run: npm install |
24 | | - |
25 | | - - name: Build React app |
26 | | - run: npm run build-react |
27 | | - |
28 | | - - name: Run Unit Tests |
29 | | - run: npm test || true # Continue even if no tests |
30 | | - |
31 | | - - name: Lint code |
32 | | - run: npm run lint || echo "No lint configured" |
33 | | - |
34 | | - - name: Deploy to Testing EC2 |
35 | | - uses: appleboy/ssh-action@v0.1.8 |
36 | | - with: |
37 | | - host: ${{ secrets.TESTING_EC2_IP }} |
38 | | - username: ubuntu |
39 | | - key: ${{ secrets.EC2_SSH_KEY }} |
40 | | - port: 22 |
41 | | - script: | |
42 | | - cd ~/react-node-testing || git clone https://github.com/${{ github.repository }} ~/react-node-testing |
43 | | - cd ~/react-node-testing |
44 | | - git pull |
45 | | - npm install |
46 | | - npm run build-react |
47 | | - |
| 13 | + - name: Checkout code |
| 14 | + uses: actions/checkout@v3 |
| 15 | + |
| 16 | + - name: Setup Node.js |
| 17 | + uses: actions/setup-node@v3 |
| 18 | + with: |
| 19 | + node-version: '18' |
| 20 | + |
| 21 | + - name: Install Dependencies |
| 22 | + run: npm install |
| 23 | + |
| 24 | + - name: Run Linting (Code Analysis) |
| 25 | + # If you don't have a specific lint command, this placeholder ensures the step exists |
| 26 | + run: echo "Linting code analysis..." |
| 27 | + |
| 28 | + - name: Run Unit Tests |
| 29 | + # runs the test script defined in package.json |
| 30 | + run: npm test |
| 31 | + |
| 32 | + - name: Build React App |
| 33 | + run: npm run build-react |
| 34 | + |
| 35 | + - name: Deploy Files to Testing Server |
| 36 | + uses: appleboy/scp-action@master |
| 37 | + with: |
| 38 | + host: ${{ secrets.TESTING_EC2_IP }} |
| 39 | + username: ubuntu |
| 40 | + key: ${{ secrets.EC2_SSH_KEY }} |
| 41 | + source: "./*" |
| 42 | + target: "/home/ubuntu/app" |
| 43 | + |
| 44 | + - name: Start Application on Testing Server |
| 45 | + uses: appleboy/ssh-action@master |
| 46 | + with: |
| 47 | + host: ${{ secrets.TESTING_EC2_IP }} |
| 48 | + username: ubuntu |
| 49 | + key: ${{ secrets.EC2_SSH_KEY }} |
| 50 | + script: | |
| 51 | + cd /home/ubuntu/app |
| 52 | + # Install production dependencies |
| 53 | + npm install --production |
| 54 | + # Restart the app using PM2 (or start if not running) |
| 55 | + pm2 restart all || pm2 start index.js --name "react-node-app" |
| 56 | + pm2 save |
0 commit comments