Skip to content

Commit dc9d538

Browse files
authored
Merge pull request #21 from lazy-ants/task-20
Extend CircleCI configuration to check the Nodejs server instance status #20
2 parents 5f018f0 + fae19dd commit dc9d538

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

.circleci/config.yml

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ jobs:
2020
sudo dpkg -i google-chrome.deb
2121
sudo sed -i 's|HERE/chrome\"|HERE/chrome\" --disable-setuid-sandbox|g' /opt/google/chrome/google-chrome
2222
rm google-chrome.deb
23+
# Install pm2
24+
- run:
25+
name: Install pm2
26+
command: sudo npm install -g pm2
27+
# Install full-icu
28+
- run:
29+
name: Install full-icu
30+
command: |
31+
sudo npm install --unsafe-perm -g full-icu
32+
echo 'export NODE_ICU_DATA=/usr/local/lib/node_modules/full-icu' >> ~/.bashrc
33+
echo 'export PATH=$NODE_ICU_DATA:$PATH' >> ~/.bashrc
34+
source ~/.bashrc
2335
# Log the current branch
2436
- run:
2537
name: Show current branch
@@ -42,6 +54,19 @@ jobs:
4254
- run:
4355
name: Building
4456
command: cd project/application && npm run build:ssr
57+
# Running the Nodejs server instance
58+
- run:
59+
name: Running the instance Nodejs server
60+
command: cd project/application && npm run serve:ssr
61+
- run: sleep 15 # workaround pm2 auto restart issue
62+
# Check the Nodejs server status
63+
- run:
64+
name: Check the Nodejs server status
65+
command: bash pm2-health-check-circleci.sh
66+
# Stop the Nodejs server
67+
- run:
68+
name: Stoping Nodejs server
69+
command: cd project/application && npm run pm2:stop
4570
# Lint the source code
4671
- run:
4772
name: Linting
@@ -59,17 +84,17 @@ jobs:
5984
machine:
6085
enabled: true
6186
environment:
62-
FINGERPRINT: 8c:c8:d3:84:56:47:d7:28:13:2a:d8:2b:4e:53:0e:c0
63-
SSH_USER: la
64-
SSH_HOST: angular-universal.lazy-ants.com
87+
FINGERPRINT: 8c:c8:d3:84:56:47:d7:28:13:2a:d8:2b:4e:53:0e:c0 # Replace this with actual value
88+
SSH_USER: la # Replace this with actual value
89+
SSH_HOST: angular-universal.lazy-ants.com # Replace this with actual value
6590
steps:
6691
- add_ssh_keys:
6792
fingerprints:
6893
- "$FINGERPRINT" #https://circleci.com/docs/2.0/add-ssh-key
6994
- run:
7095
name: Deploy Over SSH
7196
command: |
72-
ssh $SSH_USER@$SSH_HOST "cd /var/www/angular-universal && bash deploy-circleci.sh"
97+
ssh $SSH_USER@$SSH_HOST "cd /var/www/angular-universal && git status && git checkout -- . && git pull origin master && bash deploy-circleci.sh" # Replace /var/www/angular-universal path with actual value
7398
7499
workflows:
75100
version: 2

deploy-circleci.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
echo 'Before checkout'
2-
git status
3-
git checkout -- .
4-
echo 'After checkout'
5-
git pull origin master
1+
#!/bin/bash
62
docker-compose up -d --build
73
docker exec -i angular-universal_nodejs npm install
84
docker exec -i angular-universal_nodejs bash -c 'npm run build:ssr'

pm2-health-check-circleci.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
SERVER_NAME='angular-universal' # Replace this with actual value
3+
4+
pm2 show $SERVER_NAME | sed -n '3p' | sed 's: ::g' | grep '│status│online│' | grep -v grep > /dev/null
5+
if [ $? != 0 ]
6+
then
7+
echo 'The instance of Nodejs server failed to start. Try to build the application manually and check the logs.'
8+
exit 1
9+
else
10+
echo 'The instance of Nodejs server successfully started.'
11+
exit 0
12+
fi

project/application/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"lint": "ng lint",
1010
"e2e": "ng e2e",
1111
"build:ssr": "npm run build:client-and-server-bundles && npm run webpack:server",
12-
"serve:ssr": "npm run silent:update && pm2 delete -s angular-universal || : && pm2 start dist/server.js --name angular-universal",
12+
"serve:ssr": "npm run silent:update && npm run pm2:stop && npm run pm2:start",
13+
"pm2:start": "pm2 start dist/server.js --name angular-universal --max-restarts 5",
14+
"pm2:stop": "pm2 delete -s angular-universal || :",
1315
"build:client-and-server-bundles": "ng run angular-universal--browser:build:production && ng run angular-universal--server:build:production",
1416
"webpack:server": "webpack --config webpack.server.config.js --progress --colors",
1517
"silent:update": "rm -rf dist && cp -r tmp dist && rm -rf tmp"

0 commit comments

Comments
 (0)