Skip to content

Merge pull request #192 from alwinsimon/main #23

Merge pull request #192 from alwinsimon/main

Merge pull request #192 from alwinsimon/main #23

# ===================================================== Production Branch CI-CD Configuration =====================================================
name: Production-Branch-Deployment-CI-CD
# Trigger the action if and only if a pull request was PUSHED or MERGED into the production branch
# This workflow will do a final CI before deploying changes to the Production server with CD
on:
push:
branches:
- production-branch
jobs:
# ========================== Production-Branch-Pre-Deployment-Tests ==========================
Production-Branch-Pre-Deployment-Tests:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]
steps:
- uses: actions/checkout@v3
# Runs the following commands on the newly created Linux instance for integration testing
# Installing Node.js at the test instance
- name: Setting up Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
# Printing the node version in the terminal
- name: Checking Node Version
run: node --version
# Installation of Node packaging Manager (npm)
- name: Installing Node packaging Manager (npm)
run: npm ci
# Setting up the environment variable from using credentials stored in GitHub actions secret
- name: Setting up environment variables
run: |
echo "MY_ENV_VARIABLE=${{ secrets.ENV_FILE }}" >> .env
# Installation of Node packaging Manager (npm)
- name: Installing project dependencies
run: npm install
# Checking if node modules were installed (Done by simply checking if the node_modules directory exists or not)
- name: Checking if node_modules directory was created after installing NPM
run: |
if [ ! -d "node_modules" ]; then
echo "Error: node_modules directory does not exist."
exit 1
fi
# Installation of Process Manager 2 (PM2)
- name: Installing Process Manager 2 (PM2)
run: npm i pm2 -g
# Start the application with Process Manager 2 (PM2)
- name: Initiating the application with Process Manager 2 (PM2)
run: pm2 start ./bin/www --name "production-server"
# Log the status of application with Process Manager 2 (PM2)
- name: Logging the status of application with Process Manager 2 (PM2)
run: pm2 status all
# ========================== Production-Branch-Deployment ==========================
Production-Branch-Deployment:
runs-on: ubuntu-latest
needs: Production-Branch-Pre-Deployment-Tests
# Deployment steps
steps:
- name: Deploying changes to Production Server
uses: appleboy/ssh-action@master
with:
host: ${{secrets.EC2_PUBLIC_IP}}
username: ubuntu
key: ${{secrets.EC2_SSH_KEY}}
script: |
echo "Connected to Production Server instance"
# Commands for deploying changes to server
cd ExpressJs-Ecommerce-webdev-project-1
sudo git pull
sudo pm2 reload all