Migrating nodejs application to circle ci 2.0
Continuous Integration and delivery is an important aspect of any successful technology-driven application development. It helps you to continuously deliver small improvements, bug fixes to your application at a faster pace. There are many open-source and commercial CI tools within the ecosystem, for example, Jenkins, Circle CI, AWS code pipeline, etc. The objective of this blog is to show how the nodejs application was migrated to the new Circle CI 2.0.Circle CI is a great build tool for continuous integration and delivery. Circle CI 2.0 release was a major architectural overhaul of the tool and introduced many great features. One of the features is workflows and its details can be found here.Intentwise has multiple applications and services built on a number of technologies. Our User Interfacing application is built on Node JS technology using gulp as the build framework deployed on Heroku cloud provider.Our application has two stages, one for building the application which runs various JS and CSS checks, unit tests and resource modifications, and the other for deploying the application to Heroku and start the application.In the new version, Circle CI has replaced the earlier circle.yml with the new file config.yml which should be within a directory .circle. Create a new folder .cicle under the root application folder. Create a new config.yml under the .cirlce folder. Here is the sample content of the config.yml
version: 2
jobs:
build:
working_directory: ~/application-web
docker:
- image: circleci/node:4.8.2
steps:
- checkout
- run:
name: update-npm
command: 'sudo npm install -g npm@5'
- restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
- run:
name: install-npm-wee
command: npm install
- run:
name: install gulp
command: sudo npm install -g gulp-cli
- save_cache:
key: dependency-cache-{{ checksum "package.json" }}
paths:
- ./node_modules
- run:
name: bower install
command: ./node_modules/bower/bin/bower install
- run:
name: Build Application
command: gulp build
deploy:
docker:
- image: buildpack-deps:trusty
steps:
- checkout
- run:
name: Deploy Master to Heroku
command: |
git push https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git master
workflows:
version: 2
build-deploy:
jobs:
- build
- deploy:
requires:
- build
filters:
branches:
only: master
The config.yml has two main sections. One for defining the jobs and the other for defining the workflow to orchestrate the jobs.
Jobs Definition
There are two jobs, one "build" for building the application and the other "deploy" for deploying the applicationThis is a simple configuration file and the following are the things to note
- Since the application uses node.js, it needs a build environment supporting node.js. Circle CI provides many build environments out of the box as docker images. The one configured above has node 4.8.2 pre-installed
docker:
- image: circleci/node:4.8.2
- "Steps" helps to define prerequisites like code checkout and any dependencies to be installed before building the application. In the above example, it is installing node modules, bower modules before building the application using gulp
- The deploy job deploys the build artifact on the Heroku application.
Workflow Definition
- Workflows are like orchestrators for defining the order of executions. Read more about workflows here.
- According to the workflow defined, build runs for every check-in on the git repository. Whereas deploy job is executed only on the master branch. "Deploy" job will not be executed before the build as the dependency is specified with the "requires" attribute.
workflows:
version: 2
build-deploy:
jobs:
- build
- deploy:
requires:
- build
filters:
branches:
only: master
Once the workflow is initiated its progress can be monitored in the workflow section.




