Contents

CI/CD Notes: Nest.JS + TravisCI + Docker

Overview

I recently got into Nest.JS as a Backend framework and found it more comfortable since I have worked with Angular for a couple of years now. So I wanted to learn the framework a bit more than your usual Beginners Tutorial.

Note
I am not going to dive deep into how to write Unit Tests (I am not a champ) or how to use Travis CI, but at some point it might be easier to let some automation take over your software tasks.

Learning Material

  • I used the Academind ‘s YouTube Tutorial of Nest.JS + MongoDB to create a CRUD App for my MongoDB Atlas Cluster.

Continuous Integration + Docker

I had some idea of using Travis-CI previous as a tool to conduct tests for code-bases but never found time to dive deep into it.

Docker has been a hot 🔥 tool around for a while and I decided upon integrating Travis and Docker with a simple aim:

Once the tests for the API pass, build a Docker image and push it to a Docker registry.

Travis-CI Configuration

At first glance Travis CI already had documentation for Docker integration and how to leverage Travis for Pushing Docker Images to a Registry which made it a tad bit easy.

However if you need to push a Docker Image to a registry you need to have a username and password or similar credentials. Exposing such credentials within a file is something one needs to avoid at all costs.

However, there is a way to pass important environment variables without having to mention them within the code base.

Credential Setup

Follow these steps:

  1. You simply create a Travis-CI Build Project for your GitHub repository and click on More Options

Travis-CI Build Project Snippet for the Github Project

  1. Navigate to Settings and find the Environment Variables section

Environment Variables Section in Project Settings

  1. Add your Docker Hub’s Credentials here i.e. DOCKER_USERNAME and DOCKER_PASSWORD

Docker Credentials stored as environment variables for every Travis CI build

The Gotcha for Docker Hub

It is easy to be fooled by assuming that our Docker Hub’s password would be the same as we often use to login into the Hub’s Website. However, that was not the case for me. Although I had added my Docker Hub’s password within the respective environment variable DOCKER_PASSWORD I was still getting Access Denial upon pushing the Docker Image for my App.

Build Log which threw Access Denied from the Docker Hub

It took me a while to figure out that Docker Hub usually will accept an Accept Token as an accepted form of credential.

Access Tokens from Docker Hub

  1. Navigate to your Docker Hub account on hub.docker.com

  2. On the top right corner click on the User Account and navigate to Account Settings

  3. Navigate to Security and click on Generate Access Token

  4. Make sure to copy the generated Access Token within your Travis-CI Environment Variables Section for DOCKER_PASSWORD

This should now make all your Travis CI builds capable to push your Docker Images to a Registry.

Configuration File

Travis CI file are written in YAML and are typically called .travis.yml

Here is mine:

  • The after_sucess tells Travis then once my NestJS tests are successful, build the Docker Image

  • The before_deploy stage tells Travis to log into the Registry. The commands within such stages are written as elements within a list for step-by-step execution. If you wish to publish your Image to Docker Hub make sure to use:

      docker login -u "$DOCKER_USERNAME" -p "$DOCKER_PASSWORD" docker.io
    
  • If for any other Registry change the server name in the end.

  • Restart your Travis Build or commit something to the repository to trigger a fresh new build.

  • Check your Docker Hub if a new Docker Image has been pushed by Travis CI.

There you go! 🎊 Your first CI-CD DIY project. Don’t forget to add the cool Travis-CI Build Status to your Repository’s README.md to flash your passing Builds.

I am not a purist so the Continuous Delivery part of pushing an Image to the Registry makes me happy. Plus, I am planning to learn how to handle deployment on Clouds so I guess I would be spared by the DevOps Lords in the Software universe.

Resources