This is an experience shared on getting a simple node application deployed into EC2 with github actions.
- Go the project directory and type the following commands
mkdir node-hello && cd node-hello
- Type
npm init
in your terminal to initiate a node project (Make sure you have node installed if not follow the guide here) - Create the index.js file (
touch index.js
) - Copy the following code for a simple node application
const express = require('express');
const app = express();
const port = 3001;
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(port, () => {
console.log('Example app listening at http://localhost:${port}');
});
- Run
npm install
- Run the app (
node index
), connect to localhost:3001 and you will see "Hello World!" - Push your application into git
-
Visit AWS management console
-
Under services go to EC2
-
Go to Launch Instace
-
Choose an Amazon Machine Image. Make sure to select an image that is compatible with Github self hosted runners.
-
Choose instace type : Go for t2.micro which is available in free tier
-
Keep configure instance as default. Go to next.
-
Add storage keep as default. Go to next.
-
Add tag keep it as default
-
Under configure security groups add the following rules
- HTTP : Source - Anywhere
- HTTPS : Source - Anywhere
- SSH : Source - Anywhere
- Custom TCP rule : Port - Port where your node application runs : Source - Anywhere
Custom TCP is used to access your node application from browser
- Create a key pair and save it locally
- Launch the instace
- SSH into your EC2 instance using downloaded key
- Install node into instance. Follow the guide here
- Install git into instance
sudo apt-get install git
- Install daemon process manager as PM2 (
npm install pm2 -g
) to run the node application forever. Read more - Clone your repo and move into directory
- Install required node modules (
npm install
) - Run the application using PM2
sudo pm2 start app.js
- Exit the SSH, get the Public IPv4 DNS add the port you are running node application on to end in our case it is 3001. Access the address through browser. Note that since we haven't SSL encryption we have to http to access the URL. Eg :
http://<Public IPv4 DNS>:3001
. Another alternative is to use reverse proxy. Read more
- Comprehensive guide on how to install self runner into EC2 instace is available here
- Once you install if command
./run.sh
doesn't initiate your self hosted runner runsh run.sh
. This will however run git runner in foreground which will cause the runner to go off line once you close the terminal. There for run withnohup sh my_script.sh &
. Typejobs
to see active jobs running on background
- Create a folder called .github/workflow inside your local node application directory
- Create workflow file with workflow folder (
touch main.yml
) - Example workflow which uses git ui or user triggered event to trigger workflow and is given bellow.
# This is a basic workflow to help you get started with Actions
name: Test Deployment
# Controls when the workflow will run
on:
repository_dispatch:
types: [start_deploy]
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
node-deploy:
# The type of runner that the job will run on
runs-on: self-hosted
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- run: cd /home/ubuntu/hello-world-node/ && git pull origin && npm install && pm2 restart index.js
- Make sure to use indentations correctly or it will fail
- Read about actions from here. If you want to learn more regarding the syntex read here
- Push the changes into remote repo
- Once you visit actions you can select your workflow under workflows and run workflow
Once we have done the configurations we can remove the public SSH port since the deployment will be handled by github runner itself.