On-Demand Build for Monorepo
About 215 wordsLess than 1 minute
This article shows how to achieve on-demand builds in a Monorepo — triggering build tasks only for modified directories, avoiding the resource waste of full builds.
Prerequisites
- Syntax guide: Pipeline.ifModify
- System default environment variables: CNB_PIPELINE_KEY
Example Configuration
For multiple packages, use a unified template + variable differentiation approach to avoid copy-paste:
.cnb.yml
main:
push:
package-1:
services:
- docker
ifModify:
- packages/package-1/**
env:
PACKAGE: package-1
stages:
- name: set docker tag
script: echo -n "${CNB_DOCKER_REGISTRY}/${CNB_REPO_SLUG_LOWERCASE}:$CNB_COMMIT_SHORT"
exports:
info: IMAGE_TAG
- name: docker build
script: cd packages/$PACKAGE && docker build -t $IMAGE_TAG .
- name: push image
script: docker push $IMAGE_TAG
package-2:
services:
- docker
ifModify:
- packages/package-2/**
env:
PACKAGE: package-2
stages:
- name: set docker tag
script: echo -n "${CNB_DOCKER_REGISTRY}/${CNB_REPO_SLUG_LOWERCASE}:$CNB_COMMIT_SHORT"
exports:
info: IMAGE_TAG
- name: docker build
script: cd packages/$PACKAGE && docker build -t $IMAGE_TAG .
- name: push image
script: docker push $IMAGE_TAGWhen there are many packages, it is recommended to extract the common stages into a template file via include, where each package only needs to declare ifModify and env to reuse it.