CI Setup for application

Let’s say we have a Node.js API that retrieves a list of books in a database. We can create a pipeline that pushes our code through three phases: build, test and deploy. A pipeline is a group of steps that are grouped by similar characteristics. With those phases our pipeline is defined by three types:

  • Project Pipeline

  • Continuous Integration Pipeline

  • Deploy Pipeline

The Project Pipeline installs dependencies, runs linters and any scripts that deal with the code. The Continuous Integration Pipeline runs automated tests and builds a distributed version of the code. Finally, the Deploy Pipeline deploys code to a designated cloud provider and environment.

The steps that the three pipelines execute are called jobs. When you group a series of jobs by those characteristics it is called stages. Jobs are the basic building block for pipelines. They can be grouped together in stages and stages can be grouped together into pipelines. Here’s an example hierarchy of jobs, stages, and pipelines:

A.) Build
 i. Install NPM Dependencies
 ii. Run ES-Linter
 iii. Run Code-Minifier
B.) Test
 i. Run unit, functional and end-to-end test.
 ii. Run pkg to compile Node.js application
C.) Deploy
 i. Production
 1.) Launch EC2 instance on AWS
 ii. Staging
 1.) Launch on local development server

In this hierarchy, all three components are considered three different pipelines. The main bullets — build, test, and deploy are stages and each bullet under those sections are jobs. Let’s break this out into a GitLab CI/CD yamlfile.

Using GitLab CI/CD

To use GitLab CI/CD, create a file called .gitlab-ci.yml at the root of the project in your GitLab repository and add the following yaml:

image: node:10.5.0
stages:
 — build
 — test
 — deploy
before_script:
 — npm install

As I mentioned earlier, GitLab CI/CD uses Runners to execute pipelines. We can define which operating system and predefined libraries we would want our Runner to be based off by using the image directive. In our instance, we will be using the latest version of Node.js for our Runner. The stagesdirective allows us to predefine a stage for the entire configuration. Jobs will be executed based off of the order listed in the stages directive. To learn more about stages you can view it here. The before_script directive is used to run a command before all jobs.

Now let’s start with our job dedicated to the Build stage. We are going to call this job build-min-code. In this job we want it to install dependencies and minify the code. We can start this off with using the script directive. The scriptdirective is a shell script that gets executed within the Runner. Then we are going to assign this job to the “build” stage. To assign a job to a stage, use the stagedirective.

build-min-code:
  stage: build
  script:
    - npm install
    - npm run minifier

Now we have a job associated with our Build stage and we are going to do that for our Test stage. Our test job is going to be called run-unit-testand we are going to use the npm script in our API to run a test npm test.

run-unit-test:
  stage: test
  script:
    - npm run test

Finally, we are going to add a job to handle our Deploy stage: deploy-production, deploy-staging. In this instance, we are going to have two different jobs for deployment (staging and production). These jobs will reflect the same layout as our previous jobs but with a small change. Currently, all of our jobs are automatically set to be triggered on any code push or branch. We do not want to have that for when we deploy our code to staging and production. To prevent that from happening we use the only directive. The only directive defines the names of branches and tags for which the job will run. The job will look like the following:

deploy-staging:
 stage: deploy
 script:
   — npm run deploy-stage
 only:
   — develop
deploy-production:
 stage: deploy
 script:
   — npm run deploy-prod
 only:
   — master   
   

In our deploy-staging job, the Runner will only execute it if there was a change to the develop branch and for deploy-production the masterbranch. Here is a screenshot below that shows a code push made to the master branch.

From this image all three stages and jobs are triggered with the exception of deploy-staging since the code push was to themaster branch. GitLab CI/CD comes with an intuitive interface to help show what jobs and stages are running and what errors are occurring in the midst of the build. Below is the final version of the .gitlab-ci.yaml file. If you wish to test this out yourself, here is the link to the example application.

image: node:10.5.0

stages:
  - build
  - test
  - deploy

before_script:
  - echo 'hello world'

build-dev-code:
  stage: build
  script:
    - npm install
  only:
    - develop

test-dev-code:
  stage: test
  script:
    - npm run test
  only:
    - develop

deploy-dev:
  stage: deploy
  script:
    - npm run deploy-dev
  only:
    - develop


build-qa-code:
  stage: build
  script:
    - npm install
  only:
    - master

test-qa-code:
  stage: test
  script:
    - npm run test
  only:
    - master

deploy-qa:
  stage: deploy
  script:
    - npm run deploy-qa
  only:
    - master    

Last updated