A quick and dirty Continuous Delivery for a NestJs Application on DigitalOcean with Gitlab CD

Photo by Javier Allegue Barros on Unsplash

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:

🗂 What I needed:

  1. a simple Continuous delivery and deploy with Gitlab
  2. 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 .gitlab-ci.yml:

stages:
- deploy

main:
stage: deploy
script:
- ssh $USER@$SERVER_URI "cd /var/www/nestjs && git reset --hard && git pull origin master && sudo systemctl restart nestjs.service
only:
- master

The second one has been a new fabulous discovery: systemd service file

I wrote nestjs service:

# vim /etc/systemd/system/nestjs.service[Unit]
Description=NestJs Server
[Service]
ExecStart=/var/www/nestjs/node_modules/.bin/nest startWorkingDirectory=/var/www/nestjs
Restart=always
RestartSec=4
User=deployer
Group=deployer
Environment=NODE_ENV=production
[Install]WantedBy=multi-user.target

(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 deployer user!

Last thing, enable nestjs service on startup:

systemctl enable nestjs.service

… and It’s done!

Now user 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
cd /nestjs-systemd
chmod u+x ./install.sh
./install.sh

cheers, happy coding 🤜🏼🤛🏾

--

--

--

Born in ’84, workaholic, engineer, clean code addicted. Always engaged in an endless battle to conquer the world. #Laravel #NestJs #Drupal #php #typescript

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Get Dark Mode in Your React Apps in a snap of your fingers.

Playing ESP32 is Easy and Fun! [Data Visualization]

TypeScript for Beginners

Must know JavaScript Array Method and properties

Basic CLI in Node.js

10 JavaScript methods I use daily as a junior web developer

Angular Deep dive into ProvidedIn scopes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Daniele Barbaro

Daniele Barbaro

Born in ’84, workaholic, engineer, clean code addicted. Always engaged in an endless battle to conquer the world. #Laravel #NestJs #Drupal #php #typescript

More from Medium

Configure Authentication in Swagger UI for JWT in .Net Core Web API

Querying JSON with jquery (HTML dom style)

Run MongoDB for development

Build a CI / CD environment with GitHub Actions【ESLint&Prettier】