A quick and dirty Continuous Delivery for a NestJs Application on DigitalOcean with Gitlab CD
I’m super lazy when I have to deploy a node app, I always forget something but it isn’t my fault 🙄 I swear.
Lately I have been having so much free time that i can study a lot.
TLDR: Build your Gitlab CD, create a user to manage node process and follow the instruction after clone nestjs-systemd
📝 The project specs was:
- a simple NestJs App
- a Digitalocean droplet
- a self hosted Gitlab repo
🗂 What I needed:
- a simple Continuous delivery and deploy with Gitlab
- a service to start/stop/restart Node
The first one is quite simple, I created a
.gitlab-ci.yml and a shell gitlab runner
Here an easy sample of
- ssh $USER@$SERVER_URI "cd /var/www/nestjs && git reset --hard && git pull origin master && sudo systemctl restart nestjs.service
The second one has been a new fabulous discovery: systemd service file
I wrote nestjs service:
# vim /etc/systemd/system/nestjs.service[Unit]
(Be sure that /etc/systemd/system/nestjs.service file had 644 mod.)
and I gave to user
deployer the ability to start/stop/restart the application
#vim /etc/sudoers.d/deployer%deployer ALL= NOPASSWD: /bin/systemctl start nestjs.service
%deployer ALL= NOPASSWD: /bin/systemctl stop nestjs.service
%deployer ALL= NOPASSWD: /bin/systemctl restart nestjs.service
(Be sure that /etc/sudoers.d/deployer file had 440 mod.)
It isn’t safe to run node as
root so I first created a
Last thing, enable nestjs service on startup:
systemctl enable nestjs.service
… and It’s done!
deployer can start nestjs service like a charm
sudo systemctl start nestjs.service
After last push on Master, the pipeline gave me:
so now I’m ready for the next episode 💪🏻.
Are you lazy? Use my script and follow the instructions!
git clone https://github.com/danielebarbaro/nestjs-systemd.git
chmod u+x ./install.sh
cheers, happy coding 🤜🏼🤛🏾