How to deploy Node.js app on AWS with GitLab

  • If everything passes in previous step, tell GitLab to deploy these changes to AWS EC2 instances only for master branch(Continuous Deployment)

Continuous Integration with GitLab

GitLab has excellent support for CI. To enable this, we need to create .gitlab-ci.yml file.

.gitlab-ci.yml to run lint and test on GitLab

Continuous Deployment with GitLab

We need to do three steps for deployment

  • Enable GitLab container to ssh into our remote servers
  • Take the latest changes from GitLab and restart server

Update .gitlab-ci.yml

Lets update our .gitlab-ci.yml file to reflect our new requirement

.gitlab-ci.yml for test and deploy to AWS
  • Line 11: We have added another stage called deploy . It will only run after successful completion of test stage.
  • Line 29–34: We would like to run this on master branch only. We want it to run a script located at deploy/deploy.sh

Enable GitLab container to ssh into AWS EC2 instance

We need to solve for two problems:

ssh -i myPemFile.pem ubuntu@<ip address of the instance>
PRIVATE_KEY <Insert the key you downloaded when creating an EC2 instance>DEPLOY_SERVERS <comma separated ip values of all the servers to which you want to deploy this code>
eval $(ssh-agent -s)
echo "$PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
host key checking when we ssh into the instance for the first time
Host *
StrictHostKeyChecking no
disable strict host key checking in ssh
DEPLOY_SERVERS=$DEPLOY_SERVERS // We defined this in GitLab
ALL_SERVERS=(${DEPLOY_SERVERS//,/ })
for server in "${ALL_SERVERS[@]}"
do
ssh ubuntu@${server} 'bash' < ./deploy/updateAndRestart.sh
done
Deploy file to ssh into AWS EC2 instances and update node code

Take the latest changes from GitLab and restart server

This is what we want after we ssh into our box.

  • npm install:The catch here is when we do non-interactive ssh into an instance. This menas that ~/.bashrc is not loaded which in turn load our nvm.sh file which loads our node. So lets load it explicitly. Here is a great article on Configuring your login sessions with dot files
  • restart server: Next problem we need to tackle is pm2 restart. We cannot run pm2 from the source folder as we are deleting this folder and cloning again. PM2 refer the old folder which is not existent and will give error. This is the reason we do not start pm2 daemon from the source repo.

--

--

--

Senior Staff Engineer @freshworks. Ex-McKinsey/Microsoft/Slideshare/SAP, Tech Enthusiast, Passionate about India. Opinions are mine

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Build a Modern Data Lake with MinIO

How can you collab remotely to develop an effective product in 4 days?

My experience with disappointment this week

Creating a simple mobile-first grid system

Authentication at Namely: Moving to the Edge

Tuning Windows File Servers

Explained: Java chaincode in Hyperledger Fabric

Bug Leakage

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhinav Dhasmana

Abhinav Dhasmana

Senior Staff Engineer @freshworks. Ex-McKinsey/Microsoft/Slideshare/SAP, Tech Enthusiast, Passionate about India. Opinions are mine

More from Medium

Node.js + AWS DynamoDB CRUD Automation API

Beginner’s Guide to AWS API Gateway

Push multiple files under a single commit through GitHub API

Top Node.Js Web Development Framework Trends For 2022