Search This Blog

2023/05/31

Jenkin FreeStyle project for node.js

 I have typescript project with express.js i will try to use jenkin with it


I added user jenkins & sangram to sudoer

run
sudo visudo

then into file add at the bottom of file

jenkins ALL=(ALL) NOPASSWD: ALL
sangram ALL = (ALL) NOPASSWD: ALL

I created a SystemD service for my node app in /etc/systemd/system/nodeapp.service


[Unit]
Description=My Node Application Server

[Service]
ExecStart=sh /home/sangram/Documents/deployments/matrimony-api/runServer.sh


RestartSec=10
Restart=always

StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodejs-my-server-example

Environment=PATH=/usr/bin:/usr/local/bin

[Install]
WantedBy=multi-user.target


You can start/stop/restart/enable with following commands

systemctl enable nodeapp.service


systemctl start nodeapp.service
systemctl stop nodeapp.service
systemctl restart nodeapp.service
systemctl status nodeapp.service

I have private repo on github ,i created a legacy token that token need to be added to github url

https://{YOUR_GITHUB_TOKEN}@github.com/gitsangramdesai/matrimony-api.git


The project should be in /home/sangram/Documents/deployments/matrimony-api for deployment

In jenkin freestyle project added following Build Script


tsc
npm run build
sudo cp -r /var/lib/jenkins/workspace/NodejsAPI/dist/* /home/sangram/Documents/deployments/matrimony-api
sudo cp /var/lib/jenkins/workspace/NodejsAPI/.env /home/sangram/Documents/deployments/matrimony-api
sudo su - sangram

sshpass -p "sangram" ssh -o StrictHostKeyChecking=no -tt sangram@192.168.0.115 <<EOF
cd /home/sangram/Documents/deployments/matrimony-api
npm i
exit
EOF

sudo systemctl restart nodeapp.service


I need to convert typescript code to javascript using tsc command ,then need to copy that to deployment directory.Then i need to run npm Install
To Run the project need to restart our newly created service

systemctl restart nodeapp.service


the shell file /home/sangram/Documents/deployments/matrimony-api/runServer.sh is as follows

cd /home/sangram/Documents/deployments/matrimony-api/
npm start

No comments:

Post a Comment