Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Build, Test, and Deploy Apps Independently From a Monorepo

DZone's Guide to

Build, Test, and Deploy Apps Independently From a Monorepo

Set up your pipeline so that when changes occur to a project in the monorepo, the CI for that corresponding project is triggered and a Docker image is built and deployed.

· DevOps Zone
Free Resource

Learn more about how CareerBuilder was able to resolve customer issues 5x faster by using Scalyr, the fastest log management tool on the market. 

In our previous blog posts, we've shared our thoughts and experiences on our approach with monorepo. We received a few questions after that blog on how CI and deploys go with the monorepo.

In this article, we will learn how to run CI and build and deploy applications independently from a monorepo. On each PR and commit, we will run tests on the service that has changed build a Docker image from it and push it to a registry. This image can then be deployed to a cluster on any supported Container Service. We will use Shippable for this scenario.

Setup

Our monorepo has two projects. Both of them are simple Node JS apps. www is a front-end app and will run selenium tests. API is the backend app, which we will test using superagent and chai. The initial setup can be a bit tricky and depends on how projects in your repository are structured. Detecting what has changed in the repository is important. We will call a shell script from shippable.yml that will detect what has changed and trigger tests. When all our test pass successfully we will build a docker image and then push it to the registry.

shippable.yml:

language: node_js

services:
  - selenium

build:
  ci:
    - ./detectChangedFolders.sh

This YAML l will run the below shell script, which will detect changes in folders (i.e., services in our repository and run tests). Also note that we are specifying all the services required in shippable.yml

detectChangedFolders.sh:

#!/bin/bash -e
export IGNORE_FILES=$(ls -p | grep -v /)

detect_changed_folders() {
 echo "detecting changes for this build"
 folders=`git diff --name-only $SHIPPABLE_COMMIT_RANGE | sort -u | awk 'BEGIN {FS="/"} {print $1}' | uniq`
 export changed_components=$folders
}

run_tests() {
 for component in $changed_components
 do
 echo "----------------------------------------------"
 echo "$component has changed"
 if ! [[ " ${IGNORE_FILES[@]} " =~ "$component" ]]; then
 echo "-------------------Running tests for $component---------------------"
 cd "$component"
 ./run.sh "$component"
 cd ..
 else
 echo "Ignoring this change"
 fi
 done
}

if [ "$IS_PULL_REQUEST" != true ]; then
 detect_changed_folders
 run_tests
else
 echo "skipping because it's a PR"
fi

1. Enable

Enable the project on Shippable. Now, when navigating to the pipelines tab, you can view your project on SPOG.

Selection_006.png

On every build triggered, the changed folders are detected and an image is built and pushed to the registry. In our example, the run.sh file that we have in every folder is responsible for triggering tests, building an image, and pushing it to the registry.

2. Deploy

Now that our images are built, we will use pipelines to deploy these images. We will begin our resources and jobs in shippable.resources.yml and shippable.jobs.yml , respectively.

Here is how we will define resources and jobs for our pipeline:

resources:
 - name: api-img
 type: image
 integration: dockerhub
 pointer:
 sourceName: "chetantarale/sampleapp"
 seed:
 versionName: master.3

 - name: www-img
 type: image
 integration: dockerhub
 pointer:
 sourceName: "chetantarale/sampleapp"
 seed:
 versionName: master.3

 - name: ddc-cluster
 type: cluster
 integration: ddc-cluster

 - name: www-options #required
 type: dockerOptions #required
 version:
 portMappings: #optional
 - "51000:51000"

 - name: api-options #required
 type: dockerOptions #required
 version:
 portMappings: #optional
 - "50000:50000"

 - name: dockerhub
 type: integration
 integration: "dockerhub"

www-img and api-img are the two images that will be built when the CI job runs. These images are the OUTs from the CI job.

Dockerhub is a resource of type integration that points to the registry where we will push our image. ddc-cluster is an integration of type cluster where we will deploy our app. www-options and api-options are the options that can be appended to a docker image to be pushed.

We will define the following jobs in our job's YAML:

  • A runCI job that builds our images.
  • Two manifest jobs to generate a manifest.
  • Two deploy jobs that deploy our app.
jobs:
 - name: monorepo_sample_runCI
 type: runCI
 steps:
 - OUT: api-img
 - OUT: www-img
 on_success:
 - script: echo 'on success !!!!!'
 on_failure:
 - script: echo 'Failed job .... :('

 - name: www-man
 type: manifest
 steps:
 - IN: www-options
 - IN: www-img
 force: true

 - name: www-deploy
 type: deploy
 steps:
 - IN: www-man
 - IN: ddc-cluster

 - name: api-man
 type: manifest
 steps:
 - IN: api-options
 - IN: api-img
 force: true

 - name: api-deploy
 type: deploy
 steps:
 - IN: api-man
 - IN: ddc-cluster

Now, push these files and add this repo as syncRepo from Shippable UI. Shippable will read the jobs and resource's YAMLs and create your pipeline.

Selection_007.png

Selection_010.png

We have successfully set up our pipeline. For any changes to a project within in the monorepo, the CI for that corresponding project is triggered and a Docker image is built and deployed.

Find out more about how Scalyr built a proprietary database that does not use text indexing for their log management tool.

Topics:
monorepo ,ci ,docker ,devops ,tutorial ,app development

Published at DZone with permission of Chetan Tarale, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}