Deploy a NestJS application in Heroku with GitLab CI/CD pipelines

How to deploy a NestJS Application in Heroku using GitLab CI/CD pipelines

Jose Luis Campos Bautista
JavaScript in Plain English

--

Introduction

In previous days I wrote a little tutorial explaining how to create a NestJS application Create an API Rest With NestJS and PostgreSQL and now I will explain how to deploy it using GitLab Pipelines.

Prerequisites

We need to create an account in Heroku and GitLab.

Please make sure that Node.js (>= 10.13.0) is installed on your operating system.

Installation

NestJS

Once installed NodeJS we doing to install NestJS CLI, Command Line Interface that help us to maintain our application.

Once installed, we going to execute the next command to create our project.

$ nest new project-name

You can find the core files in the src directory.

Core files
  • app.controller.ts: A basic controller with a simple route.
  • app.service.ts: A simple service.
  • app.module.ts: The root module of the application.
  • main.ts: The entry file of the application which uses the core function NestFactory to create a Nest application instance.

To start the application type the next commands.

$ cd project-name$ npm run start

Now you can open your browser and navigate to http://localhost:3000.

We will create a service to manage the configuration of each environment and read the environment variables of process.env, in our case we only need the port.

$ nest g service configuration/configuration
Configuration service.

Now create an instance of the service in AppModule class and add a new static attribute called port, in the constructor initialize this attribute.

AppModule.

In the main.ts file add the port configuration

main.ts file

These are all setting that we need to deploy our application.

Heroku

Is time to install Heroku, you can find how to install it https://devcenter.heroku.com/articles/heroku-cli#download-and-install.

Once installed login into your account.

$ heroku login$ heroku create application-name

Now we need to configure an environment variable called NPM_CONFIG_PRODUCTION into false, it’s because our pipeline needs to generate the build of our project before to start the server, and PORT its the port that the app uses for deployment.

Environment variable

GitLab

Is time to create an account in GitLab, once the account was created we going to create a new repository.

Now got to the NestJS project and add the first commit and push the changes in the new repository.

$ cd projectname$ git add -A .$ git commit -m"First commit"$ git remote add origin git@gitlab.com:groupname/projectname-api.git$ git push -u origin — all

Now go to the Settings / CI/CD / Variables, add APP and HEROKU_API_KEY variables.

Environment variables

To obtain the API key of Heroku go to Account Settings and bottom you can find the value and APP is the application's name.

Heroku API key.

Now in the project create the .gitlab-ci.yml file, in this file we going to configure our pipelines.

In our pipeline, we have 4 specifications.

  • image: specifying the version of NodeJS.
  • before_script: Override a set of commands that are executed before the job.
  • stages: Defines a job stage.
  • staging: It’s our environment to deploy.

We install dpl dependency, dpl is a deploy tool made for continuous deployment that’s and developed and used by Travis CI but can also be used with GitLab CI/CD, you can find more information about it in https://docs.gitlab.com/ee/ci/examples/deployment/.

In the section of staging, we specify the type of the task “Deploy”, the image that we use, define the job stage and the script that we use to deploy.

dpl --provider=heroku --app=$HEROKU_APP_STAGING --api-key=$HEROKU_API_KEY

In the script we specify the provider, in our case is Heroku, the name of our app, and the API key.

Now create the Procfile and add the next line:

web: npm run start:prod

This command starts the project in production mode.

In the package.json we add new scripts.

"postinstall": "npm run prestart:prod""prestart:prod": "rimraf dist && npm run build"

After the install command was finished the next command is prestart:prod its because we need to create the build of our project before executing start:prod.

The configurations were finished now we commit all the changes and create a new branch called staging and push it to GitLab.

GitLab will recognize our pipeline and start with the deployment of our app in Heroku.

Deploying app.
Log.

Once the process of deployment finished we could open out the app by clicking the button Open App.

You could find the project in the https://gitlab.com/speakchron/speakchron-api

Conclusion

Let’s look at what we have learned.

  • How to create a simple application with NestJS CLI.
  • Configure environment variables in our application.
  • Configure scripts of our project to deploy.
  • The configurations of the environment on Heroku and GitLab.

First of all thanks for reading and I hope this little tutorial will help you.

Kindest Regards.

--

--