Using CircleCI with Monorepos

Took some messing around to figure out a neat way to do this so I thought I’d share it.

Create a .circleci/config.yml with a default “build” job to determine which sub-directories have changed (based on `git diff`), then use a CircleCI API to trigger jobs for the relevant projects:

version: 2
jobs:
build:
docker:
- image: circleci/golang
environment:
O: <your github organization>
R: <your github repo>
steps:
- checkout
- run:
name: Determine which directories have changed
command: |
git diff --no-commit-id --name-only -r `git log -n 2 --oneline --pretty=format:"%h" | tail -n1` | cut -d/ -f1 | sort -u > projects
printf "Modified directories:\n"
cat projects
while read project; do
if grep -Fxq $project projects.txt; then
printf "\nTriggerring build for dir: "$dir
curl -s -u ${CIRCLE_TOKEN}: \
-d build_parameters[CIRCLE_JOB]=${project} \ https://circleci.com/api/v1.1/project/github/$O/$R/tree/$CIRCLE_BRANCH
fi
done <projects
project1:
... job to build project1
project2:
... job to build project2

Add a file `projects.txt` with the names of the sub-directories that contain your projects (this will filter out unwanted directories like .circleci):

project1
project2
...

Enjoy!

Update: Please see a working example here: https://github.com/Tufin/circleci-monorepo

Tufin CTO & Co-Founder

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store