Skip to content

Instantly share code, notes, and snippets.

@sagar285
Last active June 23, 2024 12:46
Show Gist options
  • Select an option

  • Save sagar285/5c57bd0c38dc978580235e748cf832f6 to your computer and use it in GitHub Desktop.

Select an option

Save sagar285/5c57bd0c38dc978580235e748cf832f6 to your computer and use it in GitHub Desktop.
Setup Github Actions on Ec2 instance with nodejs api
#step 1- Push Your code into Github
#step2 - Create aws instance
#step3 - connect to aws instance
#step4 - Go to Setting in Your Repository
#step5 - Click on Action - choose runners
#step 6 - New Self-Hosted runners
#step7 - choose your image runner ( linux if you used aws UBUNTU)
#step8 - Now You will get some command in Downlad section .
#step 9 -Copy and paste those command one by one in aws connection .
step 10 - Go to confguration and in same way copy and pasted in aws connect
step 10 - in aws ask some question you can leave is default or give name ( next steps based on leave dfault )
step 11 - after givig all asnwer . you should come on runner in repo setting . now one runner generated in your repo
step 12 - go to folder -> cd actions-runner (or if you give other name of folder )
step 13 - run command in aws ec2 connect -> sudo ./svc.sh install
step 14 -- after install run command -> sudo ./svc.sh start (this will run your action now condition should be idle in you repo)
step 15 - add env variables -> go to setting in repo -> secret variables -> Actions -> new repository secret -> Give your file name ->pasted all env variables
step 16 -> Go to Actions tab in Repository ( not in setting) -> Go to Continous integration -> Nodejs -> Configure
step17 - change CI to CI/CD and runs-on :self-hosted ( because this is self-hosted runner) and select you nodejs version
# this is how my file looks you can check and verify if you want ( PROD_ENV_FILE - this is env file which i give in secret variables)
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
name: Node.js CI/CD
on:
push:
branches: [ "master" ]
jobs:
build:
runs-on: self-hosted
strategy:
matrix:
node-version: [18.x,20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: |
touch .env
echo "${{secrets.PROD_ENV_FILE}}" > .env
step 18 --- in aws connect go to actions-runner folder and check for _work folder go in that folder you found your repository folder name
go in that folder
step19 - now you will see all your files availabe in yourrepo by using command (ls)
step 20 - used nginx and pm2 for web server proxy and process manager
sudo npm i pm2 -g
pm2 start index (your fie name)
## 7. Install NGINX and configure
```
sudo apt install nginx
sudo vim /etc/nginx/sites-available/default
server_name yourdomain.com ;
location / {
proxy_pass http://localhost:5000; #whatever port your app runs on
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
# Check NGINX config
sudo nginx -t
step 21 - just change in .github/worflows/node.js.yml
add this line --
- run: pm2 restart server
now you file look like this
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
name: Node.js CI/CD
on:
push:
branches: [ "master" ]
jobs:
build:
runs-on: self-hosted
strategy:
matrix:
node-version: [18.x,20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: |
touch .env
echo "${{secrets.PROD_ENV_FILE}}" > .env
- run: pm2 restart server
Congrats you setup your self hosted runner now you can push your code and this directly reflected to you code πŸ‘Œβœ”πŸ†’
Limitation of self hosted runner --
https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/about-self-hosted-runners#usage-limits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment