-
Notifications
You must be signed in to change notification settings - Fork 5
103 lines (71 loc) · 3.03 KB
/
Production-Branch-Deployment-CI-CD.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# ===================================================== 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