diff --git a/.github/workflows/deploy-testing.yml b/.github/workflows/deploy-testing.yml index 143821f9..3e4b2b2e 100644 --- a/.github/workflows/deploy-testing.yml +++ b/.github/workflows/deploy-testing.yml @@ -1,47 +1,56 @@ -name: Deploy to Testing +name: Deploy to Testing Environment on: pull_request: - branches: - - main - workflow_dispatch: # Manual trigger button + branches: [ main ] + workflow_dispatch: # Allows manual trigger button jobs: - build-test-deploy: + build-and-test: runs-on: ubuntu-latest - + steps: - - name: Checkout code - uses: actions/checkout@v3 - - - name: Setup Node.js - uses: actions/setup-node@v3 - with: - node-version: 20 - - - name: Install dependencies - run: npm install - - - name: Build React app - run: npm run build-react - - - name: Run Unit Tests - run: npm test || true # Continue even if no tests - - - name: Lint code - run: npm run lint || echo "No lint configured" - - - name: Deploy to Testing EC2 - uses: appleboy/ssh-action@v0.1.8 - with: - host: ${{ secrets.TESTING_EC2_IP }} - username: ubuntu - key: ${{ secrets.EC2_SSH_KEY }} - port: 22 - script: | - cd ~/react-node-testing || git clone https://github.com/${{ github.repository }} ~/react-node-testing - cd ~/react-node-testing - git pull - npm install - npm run build-react - + - name: Checkout code + uses: actions/checkout@v3 + + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: '18' + + - name: Install Dependencies + run: npm install + + - name: Run Linting (Code Analysis) + # If you don't have a specific lint command, this placeholder ensures the step exists + run: echo "Linting code analysis..." + + - name: Run Unit Tests + # runs the test script defined in package.json + run: npm test + + - name: Build React App + run: npm run build-react + + - name: Deploy Files to Testing Server + uses: appleboy/scp-action@master + with: + host: ${{ secrets.TESTING_EC2_IP }} + username: ubuntu + key: ${{ secrets.EC2_SSH_KEY }} + source: "./*" + target: "/home/ubuntu/app" + + - name: Start Application on Testing Server + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.TESTING_EC2_IP }} + username: ubuntu + key: ${{ secrets.EC2_SSH_KEY }} + script: | + cd /home/ubuntu/app + # Install production dependencies + npm install --production + # Restart the app using PM2 (or start if not running) + pm2 restart all || pm2 start index.js --name "react-node-app" + pm2 save