Search This Blog

2023/09/06

GitHub CI/CD using GitHub Action for node.js api

create a typescript-express project add some routes to test ,here we are not using in database

Commit your project to GitHub.Then select repository & go to action and select Node.js from available option and create pipeline file.

file name in my case is node.js.yml

Change its content to below

# 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

on:
push:
branches: ["main"]
jobs:
build:
runs-on: self-hosted

strategy:
matrix:
node-version: [20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
- run: npm ci
- run: rm -rf dist
- run: npm run build --if-present
- run: |
cd dist/src
pm2 delete all
pm2 start ../bin/www.js
env:
CI: true


Now go to setting action runner create runner there it will list some command to be run on server i am using aws instance with ubuntu

there run this commands in home folder.to test project running on 3000 port you need to open port in AWS security group.

Now it will pool your repo code on server in case of default action_runner/_work/your_repo/your_repo folder.to

in my pipeline code i am converting typescript code to node.js then running pm2 on enterpoint javascript.

to test add one route in your code and commit ,push.then same changes should reflect on server.

e.g. GET http://yourip/testroute should give you result.

you can also install nginx add modify its /etc/nginx/site-available/default file by

location /api/ {

proxy_pass http://localhost:3000/;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}
then you can access your route on http://yourip/api/testroute

My code :https://github.com/gitsangramdesai/typescript-express-api

No comments:

Post a Comment