Continuous Deployment using GitHub Actions

In this tutorial we will learn about Continuous Deployment for a Node(.js)/Express(.js) app inside EC2 instance using GitHub Actions. First create an EC2 instance with Ubuntu (linux) Operating System. Connect to that instance via SSH. But before that type,

chmod 400 {pem-key}.pem

This is for giving the read permission to (.)pem key. Then connect the instance with that command below, (copy your SSH command, which will be like below)

ssh -i "{pem-key}.pem"

After successfully entering the instance, type this command inside root folder (/home/ubuntu),

sudo apt update

Let’s install Node, NPM & Nginx using sudo apt command,

sudo apt install nginx nodejs npm

You can verify the node(.js) version by typing node -v.

To verify NGINX type these commands,

sudo service nginx start
sudo service nginx status

Most probably you will see an active status for NGINX.

Then install pm2 via sudo npm i -g pm2, which is a process manager for Node(.js), in order to keep our application alive forever.

Now let’s move to our repository. Let’s navigate to server repository. Inside of that repository click the Actions tab. Type Node and select Node(.js) workflow.

Add these two lines inside of .github -> workflows -> node.js.yml.

- run: npm run build --if-present
- run: sudo pm2 restart server
- run: sudo systemctl restart nginx

As of now let’s remove - run: npm run test since we are focusing Continuous Deployment.

Now go to Settings -> Actions -> Runners and click New Self Hosted Runner. (Runners are the machines that execute jobs in a GitHub Actions workflow)

Chose the Linux platform,

After selecting Linux you will see some commands(which we will use that inside our instance).

Now go the our instance and inside the root folder type,

mkdir server && cd server

After navigating to server folder, type

curl -o actions-runner-linux-x64-2.307.1.tar.gz -L

This command will download the latest runner package. Then type this,

echo "038c9e98b3912c5fd6d0b277f2e4266b2a10accc1ff8ff981b9971a8e76b5441  actions-runner-linux-x64-2.307.1.tar.gz" | shasum -a 256 -c

After that,

tar xzf ./actions-runner-linux-x64-2.307.1.tar.gz

This will extract the installer. Then type,

./ --url{your_github_username}/{name_of_the_repo} --token {token}

You can find yours in the configure section.

Then type sudo ./ install and sudo ./ start

Now refresh your runner’s page you will see the new runner has been created.

Change something of your code and push to your github repository. Then inside of the instance server folder you will see _work folder has been created.

Then navigate to this _work directory and inside of this directory you can see server folder. Navigate to this. Inside of this you may see another server directory, navigate to this.

After that start pm2,

sudo pm2 start app.js --name=server

app.js is our entry file.

This is how you can integrate Continuous Deployment using GitHub Action.

Leave a Comment

Your email address will not be published. Required fields are marked *