Complete CI/CD in Node JS using Docker, CircleCI, AWS (Part 3— Linking CircleCI and Github).

Gaurav
theserverfault
Published in
4 min readOct 9, 2019

--

In the past two sections, we’ve discussed how we can structure our code to handle the development and testing process to make our code the best compliant for CI/CD process. In this section, we’ll explore how we can create a link between our git repository and the CircleCI console to make it ready to handle the automated build pipeline triggers whenever there is a new code push to a specified repository branch.

CircleCI VCS Integration Workflow/Pipeline

CircleCI handles the configurations via a config.yml file that follows the YAML format to create configurations. The presence of a .circleci/config.yml file in your CircleCI authorized repository branch indicates that you want to use the 2.x infrastructure. You can find the complete config.yml documentation on the CircleCI website https://circleci.com/docs/2.0/configuration-reference/.

The configuration file

Let’s cut the shit and get started with writing out the first config.yml file and go through its breakdown step by step. Following is the sample config.yml with the node and docker build support for javascript.

Note: The config file is created with reference to the dockerfile created in the last blog.

# Javascript Node CircleCI 2.0 configuration file## Check https://circleci.com/docs/2.0/language-javascript/ for more details#version: 2
jobs:
build: branches: only: - master - development
docker:
# specify the version you desire here - image: circleci/node:8.0 - image: docker:17.09.1-ce-git # enable the docker build support
working_directory: ~/repo
steps: - checkout - setup_remote_docker # Download and cache dependencies - restore_cache: keys: - v1-dependencies-{{ checksum "package.json" }} # fallback to using the latest cache if no exact match is found - v1-dependencies-

# clearing the cache
- run: npm cache clear --force # - run: rm package-lock.json - run: npm install - save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
# run tests!
- run: npm run test
# build the docker image on success - run: name: Build Success when: on_success command: | docker --version docker login -u=$DOCKER_LOGIN -p=$DOCKER_PASSWORD docker build -t <docker_account>/<application_name>:$CIRCLE_BRANCH --build-arg MACHINE_NAME=<application_name>-$CIRCLE_BRANCH . docker push <docker_account>/<application_name>:$CIRCLE_BRANCH echo "Docker build made sucessfully!! for <application_name> $CIRCLE_BRANCH" - run: name: Build Failure when: on_fail command: | echo "ERROR building <application_name> $CIRCLE_BRANCH"

This is the sample config.yml file that must be placed under the.circleci folder in the root folder of your project. This will help CircleCI to identify that this branch is configured for the CircleCI build pipeline.

Here in this configuration, we’ve done the following things:

  • Configured the config file to use the CircleCI 2.0 Configurations/Architecture.
  • Configured the CircleCI to build only the master and development branches of our target GitHub repository.
  • Configured the node runtime environment for CircleCI and Docker runtime environment for docker builds.
  • Specified a working directory by ~/repo that will map to the repository payload that GitHub will send to CircleCI on every new push to the configured branches.
  • Specified the build steps that will involve checking out of code, handling caches, installing node_modules, running unit tests, etc. etc.
  • Building the docker image on successful unit tests and pushing the docker image to the project Docker repository.

NOTE:
You might’ve noticed the usage of $DOCKER_LOGIN, $DOCKER_PASSWORD here. They are the global variables that are configured through Environment Variables. You can see how to configure them through Project settings > Environment Variables and providing a key-value pair.
There are few other environment variables like $CIRCLE_BRANCH which are automatically set-up by CircleCI and however do not require manual definition.

Configuring the CircleCI console

Now after creating the .circleci/config.yml file in our repository and pushing the changes onto the git repository. Now is the time to configure our project on the CircleCI console. You can log in to CircleCI via GitHub or bitbucket and in this tutorial, we’ve logged in via GitHub. After logging in, you will see the navigation section on the left side of the window that has the “Add Projects” option. You’ll see an option to “SetUp Project” which is not already configured on CircleCI.

Clicking will take you to the next screen where you have to choose your operating system. My Organization prefers building in Linux so I’ll choose Linux and also choose the Language that you are building for. We’ll choose Node. This page will also give you instruction about how to place config.yml file in your project root directory and a sample config.yml as well.

And once you start on Start Building, your project will immediately start building. You can see the progress of your build-ins Jobs section.

After a successful build, The docker image will be pushed to the projects Docker account with the branch tag and you’ll get an email notification regarding the successful build of the requested job.

Here in this part, we have covered the Continuous Integration Part of the CI/CD pipeline.

Note for the next:

In the last and final part of this series, we’ll set up our AWS servers to configure the Continuous Deployment of the latest built Docker images that would be done automatically and without any human intervention.

References

--

--

Gaurav
theserverfault

The mass of men leads a life of quiet desperation. I just want to live deep and suck out all the marrow of life. https://www.theserverfault.,com