CareerByteCode
ByteBlogby CareerByteCode

Monorepo CI/CD: Build Only What Changed

What you build: a monorepo pipeline on Cloud Build that builds, tests and deploys only the changed service to Cloud Run, rebuilds all on a shared library change, plus 2 real breaks: no .git and a docs

Monorepo CI/CD: Build Only What Changed

✍️ Translate this blog into Hindi, Tamil or Telugu

A monorepo keeps many services in one Git repository: one place for code, one history, easy changes across services. Its classic CI/CD problem: a one-line change in one service rebuilds and redeploys everything, so pipelines get slow and every deploy touches services that did not change. In this lab you build a monorepo pipeline on Cloud Build that works out which services changed and builds, tests and deploys only those to Cloud Run, and rebuilds every service when shared code changes. On the way you hit two real problems: the pipeline cannot see Git history at all, and a change to the README makes the pipeline fail.

Who it is for: DevOps and backend engineers who know basic Git and Docker. Time: about 1 to 1.5 hours. Cost: inside free tiers for this lab (Cloud Build's free build minutes, Cloud Run, a few small images). We delete everything at the end.

Every command below was run for real by the CareerByteCode Team on a fresh Google Cloud project, and every output is the real result we got. Commit ids and build ids will differ on your machine.

🧪⭐ Recommended by CareerByteCode · ByteLabs project bundleCloud and DevOps Realtime Starterโ‚น499Build the projects →

Part A: monorepo CI/CD in plain English

Monorepo pipeline in one picture
git diff decides: a changed service is built alone, a changed shared library rebuilds every service, docs build nothing

Our repository:

services/api/     the api service (Flask) with its tests and Dockerfile
services/web/     the web service with its Dockerfile
libs/common/      a small library BOTH services use
ci/changed.sh     decides what to build
cloudbuild.yaml   the pipeline

The rule in ci/changed.sh, using git diff --name-only <before> HEAD (the list of files changed since the last built commit):

A file changed underBuild
services/<name>/that service only
libs/every service (they all use it)
anything else (README, docs)nothing

Real-life example: an apartment building. If one flat's kitchen is repaired, only that flat gets an inspection. If the shared water pipe is replaced, every flat is inspected. A new doormat needs no inspection.

Words you will use:

  • Base commit: the commit the last pipeline built. We pass it to the pipeline as _BASE.
  • Build context: the files Cloud Build receives. A .gcloudignore file decides what is uploaded.
  • Substitutions: variables in cloudbuild.yaml ($_BASE, $_SHA, $PROJECT_ID). A shell dollar sign inside the file is written $$.

The script and the pipeline:

#!/bin/bash
# ci/changed.sh <base-commit> : prints the services that must be built for the commits after <base-commit>.
#   a file under services/<name>/  -> that service
#   a file under libs/             -> every service (they all use the shared library)
#   anything else (README, docs)   -> nothing
set -euo pipefail
BASE=${1:-}
ALL=$(ls services)
if [ -z "$BASE" ]; then echo $ALL; exit 0; fi          # first build: build everything
FILES=$(git diff --name-only "$BASE" HEAD)
echo "files changed since ${BASE:0:7}:" $FILES >&2
if echo "$FILES" | grep -q '^libs/'; then echo $ALL; exit 0; fi
echo "$FILES" | grep '^services/' | cut -d/ -f2 | sort -u | tr '\n' ' '
# Monorepo pipeline: 1 work out which services changed, 2 test them, 3 build and push only those images, 4 deploy only those.
# $$ = a dollar sign for the shell (a single $ is a Cloud Build substitution like $PROJECT_ID or $_SHA).
steps:
- id: changed
  name: gcr.io/cloud-builders/git
  entrypoint: bash
  args: ["-c", "git config --global --add safe.directory /workspace && bash ci/changed.sh '$_BASE' > /workspace/changed.txt && echo \"to build: $$(cat /workspace/changed.txt)\""]
- id: test
  name: python:3.12-slim
  entrypoint: bash
  args: ["-c", "pip install -q flask==3.1.* pytest 2>/dev/null; for s in $$(cat /workspace/changed.txt); do if [ -d services/$$s/tests ]; then (cd services/$$s && cp -r ../../libs/common . && python -m pytest -q tests) || exit 1; fi; done; echo tests done"]
- id: build
  name: gcr.io/cloud-builders/docker
  entrypoint: bash
  args: ["-c", "for s in $$(cat /workspace/changed.txt); do docker build -q -f services/$$s/Dockerfile -t us-east1-docker.pkg.dev/$PROJECT_ID/mono/$$s:$_SHA . > /dev/null && docker push -q us-east1-docker.pkg.dev/$PROJECT_ID/mono/$$s:$_SHA > /dev/null || exit 1; echo built and pushed $$s; done"]
- id: deploy
  name: gcr.io/google.com/cloudsdktool/cloud-sdk:slim
  entrypoint: bash
  args: ["-c", "for s in $$(cat /workspace/changed.txt); do gcloud run deploy mono-$$s --image=us-east1-docker.pkg.dev/$PROJECT_ID/mono/$$s:$_SHA --region=us-east1 --allow-unauthenticated --set-env-vars=COMMIT=$_SHA --max-instances=1 --quiet 2>&1 | tail -1; [ $${PIPESTATUS[0]} -eq 0 ] && echo deployed mono-$$s || exit 1; done; [ -s /workspace/changed.txt ] || echo 'nothing changed in services or libs: nothing to deploy'"]
substitutions:
  _BASE: ""
  _SHA: "manual"
options:
  logging: CLOUD_LOGGING_ONLY

Each service's Dockerfile builds from the repository root, so it can copy the shared library: docker build -f services/api/Dockerfile ..

Before you start

  1. A Google Cloud account (the free trial works) and a project.
  2. Open Cloud Shell (the >_ icon at the top right of the console) and turn on the APIs:
export PROJECT_ID="your-project-id"
gcloud config set project $PROJECT_ID
gcloud services enable cloudbuild.googleapis.com run.googleapis.com artifactregistry.googleapis.com
  1. Create the folder ~/monorepo with these files: ci/changed.sh and cloudbuild.yaml from above, and the six files below (the exact files from our lab).

~/monorepo/libs/common/greet.py:

# libs/common/greet.py: shared code used by BOTH services. A change here must rebuild both.
VERSION = "1.0"


def banner(service):
    return f"{service} (common lib {VERSION})"

~/monorepo/services/api/main.py:

# services/api/main.py: the "api" service. GET / returns its version and the shared library version.
import os
from flask import Flask, jsonify
from common.greet import banner

API_VERSION = "1.0.0"
app = Flask(__name__)


@app.get("/")
def home():
    return jsonify(service=banner("api"), version=API_VERSION, commit=os.environ.get("COMMIT", "local"))

~/monorepo/services/api/tests/test_api.py:

from main import app


def test_home():
    r = app.test_client().get("/")
    assert r.status_code == 200
    assert r.get_json()["service"].startswith("api")

~/monorepo/services/api/Dockerfile:

# Built from the REPO ROOT (so it can copy the shared library): docker build -f services/api/Dockerfile .
FROM python:3.12-slim
WORKDIR /app
RUN pip install --no-cache-dir flask==3.1.* gunicorn==23.*
COPY libs/common ./common
COPY services/api/main.py .
ENV PYTHONUNBUFFERED=1
CMD exec gunicorn --bind :$PORT --workers 1 --threads 4 main:app

~/monorepo/services/web/main.py:

# services/web/main.py: the "web" service. GET / returns a small HTML page.
import os
from flask import Flask
from common.greet import banner

WEB_VERSION = "1.0.0"
app = Flask(__name__)


@app.get("/")
def home():
    return f"<h1>Shop web {WEB_VERSION}</h1><p>{banner('web')}, commit {os.environ.get('COMMIT', 'local')}</p>\n"

~/monorepo/services/web/Dockerfile:

# Built from the REPO ROOT (so it can copy the shared library): docker build -f services/web/Dockerfile .
FROM python:3.12-slim
WORKDIR /app
RUN pip install --no-cache-dir flask==3.1.* gunicorn==23.*
COPY libs/common ./common
COPY services/web/main.py .
ENV PYTHONUNBUFFERED=1
CMD exec gunicorn --bind :$PORT --workers 1 --threads 4 main:app

Part B: the lab

Step 1: put the monorepo in Git

# A monorepo: two services (api, web), one shared library, a CI script and one pipeline file, all in ONE Git repository
git config --global user.name "Lab Student"; git config --global user.email "lab@example.com"; git config --global init.defaultBranch main
cd ~ && rm -rf repo && cp -r monorepo repo && cd repo && chmod +x ci/changed.sh
git init -q && git add . && git commit -qm "Monorepo: api + web + common lib" && git log --oneline
find . -path ./.git -prune -o -type f -print | sort
0200dc3 Monorepo: api + web + common lib
./ci/changed.sh
./cloudbuild.yaml
./libs/common/greet.py
./services/api/Dockerfile
./services/api/main.py
./services/api/tests/test_api.py
./services/web/Dockerfile
./services/web/main.py

Step 2: an image repository and a pipeline account

Why: the pipeline runs as its own service account with only five roles (deploy to Cloud Run, act as the runtime account, push images, write logs, read the build source). New roles take a little time to work everywhere, so a tiny probe build retries until the account can run builds and write logs.

# An image repository and a pipeline service account (mono-pipeline) with only the roles the pipeline needs
gcloud artifacts repositories create mono --repository-format=docker --location=us-east1 --description="monorepo images" 2>&1 | tail -1
gcloud iam service-accounts create mono-ci-1121 --display-name="CI/CD pipeline" 2>&1 | tail -1
until gcloud iam service-accounts describe mono-ci-1121@$PROJECT_ID.iam.gserviceaccount.com > /dev/null 2>&1; do sleep 5; done
for r in roles/run.admin roles/iam.serviceAccountUser roles/artifactregistry.writer roles/logging.logWriter roles/storage.objectViewer; do
  until gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:mono-ci-1121@$PROJECT_ID.iam.gserviceaccount.com" --role=$r --condition=None --format="value(etag)" > /dev/null 2>&1; do sleep 5; done; echo "mono-ci-1121: $r"
done
# New roles can take a few minutes to work. A tiny probe build retries until the account can run builds AND write logs
cd ~
printf 'steps:\n- name: bash\n  args: ["-c", "echo the pipeline account works"]\noptions:\n  logging: CLOUD_LOGGING_ONLY\n' > probe.yaml
for i in $(seq 1 20); do PID_=$(gcloud builds submit --no-source --config=probe.yaml --region=us-east1 --service-account=projects/$PROJECT_ID/serviceAccounts/mono-ci-1121@$PROJECT_ID.iam.gserviceaccount.com --async --format="value(id)" 2>/dev/null); sleep 30; gcloud builds log $PID_ --region=us-east1 2>/dev/null | grep -q "pipeline account works" && break; sleep 20; done; echo "probe build: $(gcloud builds describe $PID_ --region=us-east1 --format='value(status)') and its log is readable, after $i tries"
Created repository [mono].
Service account email: mono-ci-1121@your-project-id.iam.gserviceaccount.com
mono-ci-1121: roles/run.admin
mono-ci-1121: roles/iam.serviceAccountUser
mono-ci-1121: roles/artifactregistry.writer
mono-ci-1121: roles/logging.logWriter
mono-ci-1121: roles/storage.objectViewer
probe build: SUCCESS and its log is readable, after 3 tries

Step 3: build 1, everything

Why: the very first build has no "before" commit (_BASE is empty), so changed.sh builds every service. --async returns the build id at once; we wait for that exact build, then read its log (builds that run as your own service account log to Cloud Logging, not to the terminal).

# Build 1: the first build has no "before" commit, so it builds, tests and deploys BOTH services
cd ~/repo
ID=$(gcloud builds submit --config=cloudbuild.yaml --region=us-east1 \
  --service-account=projects/$PROJECT_ID/serviceAccounts/mono-ci-1121@$PROJECT_ID.iam.gserviceaccount.com \
  --substitutions=_SHA=$(git rev-parse --short HEAD),_BASE= . --async --format="value(id)" 2>/dev/null)
for i in $(seq 1 90); do gcloud builds describe $ID --region=us-east1 --format="value(status)" | grep -qE "SUCCESS|FAILURE|TIMEOUT|CANCELLED" && break; sleep 10; done; sleep 20
echo "build ${ID:0:8}: $(gcloud builds describe $ID --region=us-east1 --format='value(status)')"
gcloud builds log $ID --region=us-east1 2>/dev/null | grep -E "to build:|passed|built and|deployed|nothing|STATUS|SUCCESS|FAILURE|ERROR" | sed -E 's/^Step #[0-9]+ - "[a-z-]+": //' | head -12
build 421bd328: SUCCESS
to build: api web
1 passed in 0.15s
built and pushed api
built and pushed web
deployed mono-api
deployed mono-web
Build 1
The first build builds, tests and deploys both services

Break it: the pipeline cannot see Git history

We change only the api service, commit, and run the pipeline with _BASE set to the previous commit.

# Break it: change ONLY the api, and build with "what changed since the last commit". The pipeline cannot see Git history
cd ~/repo
sed -i 's/API_VERSION = "1.0.0"/API_VERSION = "1.1.0"/' services/api/main.py && git commit -qam "api 1.1.0" && git log --oneline -1
ID=$(gcloud builds submit --config=cloudbuild.yaml --region=us-east1 \
  --service-account=projects/$PROJECT_ID/serviceAccounts/mono-ci-1121@$PROJECT_ID.iam.gserviceaccount.com \
  --substitutions=_SHA=$(git rev-parse --short HEAD),_BASE=$(git rev-parse HEAD~1) . --async --format="value(id)" 2>/dev/null)
for i in $(seq 1 90); do gcloud builds describe $ID --region=us-east1 --format="value(status)" | grep -qE "SUCCESS|FAILURE|TIMEOUT|CANCELLED" && break; sleep 10; done; sleep 20
echo "build ${ID:0:8}: $(gcloud builds describe $ID --region=us-east1 --format='value(status)')"
gcloud builds log $ID --region=us-east1 2>/dev/null | grep -iE "to build:|fatal|not a git|usage: git diff|STATUS|SUCCESS|FAILURE" | sed -E 's/^Step #[0-9]+ - "[a-z-]+": //' | head -6
906ebd6 api 1.1.0
build c11d68ac: FAILURE
ERROR: build step 0 "gcr.io/cloud-builders/git" failed: step exited with non-zero status: 1

The build fails in its very first step (git). The full build log (gcloud builds log <build id> --region=us-east1) says error: Could not access '0200dc35...': git cannot find the base commit. The reason: gcloud builds submit does not upload the .git folder by default. Without a .gcloudignore file it uses a built-in one that skips .git, so inside Cloud Build there is no history to diff, and git diff stops with an error.

Fix it: a .gcloudignore that keeps .git

Why: once a .gcloudignore file exists, gcloud uses only its rules. Ours skips Python caches and nothing else, so .git is uploaded. We build with _BASE two commits back so the api change is included.

# Fix it: by default gcloud builds submit does NOT upload the .git folder. A .gcloudignore file decides what is uploaded
cd ~/repo
printf '# upload everything, INCLUDING .git (the pipeline needs the history to see what changed)\n__pycache__/\n.pytest_cache/\n' > .gcloudignore && cat .gcloudignore
git add .gcloudignore && git commit -qm "Upload .git to Cloud Build" && git log --oneline -3
ID=$(gcloud builds submit --config=cloudbuild.yaml --region=us-east1 \
  --service-account=projects/$PROJECT_ID/serviceAccounts/mono-ci-1121@$PROJECT_ID.iam.gserviceaccount.com \
  --substitutions=_SHA=$(git rev-parse --short HEAD),_BASE=$(git rev-parse HEAD~2) . --async --format="value(id)" 2>/dev/null)
for i in $(seq 1 90); do gcloud builds describe $ID --region=us-east1 --format="value(status)" | grep -qE "SUCCESS|FAILURE|TIMEOUT|CANCELLED" && break; sleep 10; done; sleep 20
echo "build ${ID:0:8}: $(gcloud builds describe $ID --region=us-east1 --format='value(status)')"
gcloud builds log $ID --region=us-east1 2>/dev/null | grep -E "files changed|to build:|passed|built and|deployed|nothing|STATUS|SUCCESS|FAILURE" | sed -E 's/^Step #[0-9]+ - "[a-z-]+": //' | head -10
# upload everything, INCLUDING .git (the pipeline needs the history to see what changed)
__pycache__/
.pytest_cache/
6190f2a Upload .git to Cloud Build
906ebd6 api 1.1.0
0200dc3 Monorepo: api + web + common lib
build ffa78381: SUCCESS
files changed since 0200dc3: .gcloudignore services/api/main.py
to build: api
1 passed in 0.15s
built and pushed api
deployed mono-api
Only api
With .git uploaded, git diff finds .gcloudignore and services/api/main.py: only api is built and deployed

files changed since ...: .gcloudignore services/api/main.py, so only api was tested, built and deployed. The web service was not touched.

Step 4: change only the web service

# Change ONLY the web service: only web is built and deployed; api is not touched
cd ~/repo
sed -i 's/WEB_VERSION = "1.0.0"/WEB_VERSION = "1.1.0"/' services/web/main.py && git commit -qam "web 1.1.0" && git log --oneline -1
ID=$(gcloud builds submit --config=cloudbuild.yaml --region=us-east1 \
  --service-account=projects/$PROJECT_ID/serviceAccounts/mono-ci-1121@$PROJECT_ID.iam.gserviceaccount.com \
  --substitutions=_SHA=$(git rev-parse --short HEAD),_BASE=$(git rev-parse HEAD~1) . --async --format="value(id)" 2>/dev/null)
for i in $(seq 1 90); do gcloud builds describe $ID --region=us-east1 --format="value(status)" | grep -qE "SUCCESS|FAILURE|TIMEOUT|CANCELLED" && break; sleep 10; done; sleep 20
echo "build ${ID:0:8}: $(gcloud builds describe $ID --region=us-east1 --format='value(status)')"
gcloud builds log $ID --region=us-east1 2>/dev/null | grep -E "files changed|to build:|passed|built and|deployed|nothing|STATUS|SUCCESS|FAILURE" | sed -E 's/^Step #[0-9]+ - "[a-z-]+": //' | head -10
13e0eeb web 1.1.0
build dcbbcead: SUCCESS
built and pushed web
deployed mono-web

Only web was built and deployed.

Step 5: change the shared library

🎓⭐ Recommended by CareerByteCode · Live trainingCloud & DevOps Live Cohort Internshipโ‚น2,360See the training →

Why: libs/common is used by both services, so a change there must rebuild both.

# Change the SHARED library: both services use it, so both are rebuilt
cd ~/repo
sed -i 's/VERSION = "1.0"/VERSION = "1.1"/' libs/common/greet.py && git commit -qam "common lib 1.1" && git log --oneline -1
ID=$(gcloud builds submit --config=cloudbuild.yaml --region=us-east1 \
  --service-account=projects/$PROJECT_ID/serviceAccounts/mono-ci-1121@$PROJECT_ID.iam.gserviceaccount.com \
  --substitutions=_SHA=$(git rev-parse --short HEAD),_BASE=$(git rev-parse HEAD~1) . --async --format="value(id)" 2>/dev/null)
for i in $(seq 1 90); do gcloud builds describe $ID --region=us-east1 --format="value(status)" | grep -qE "SUCCESS|FAILURE|TIMEOUT|CANCELLED" && break; sleep 10; done; sleep 20
echo "build ${ID:0:8}: $(gcloud builds describe $ID --region=us-east1 --format='value(status)')"
gcloud builds log $ID --region=us-east1 2>/dev/null | grep -E "files changed|to build:|passed|built and|deployed|nothing|STATUS|SUCCESS|FAILURE" | sed -E 's/^Step #[0-9]+ - "[a-z-]+": //' | head -10
3692878 common lib 1.1
build 6819b11d: SUCCESS
files changed since 13e0eeb: libs/common/greet.py
to build: api web
1 passed in 0.14s
built and pushed api
built and pushed web
deployed mono-api
deployed mono-web
Web and shared library
A web-only change builds web; a change in libs/common rebuilds both services

Break it again: only the README changed

Nothing should be built. But watch what the pipeline does:

# Break it again: change only the README. Nothing should be built, but watch what the pipeline does
cd ~/repo
echo "# Shop monorepo: services/api, services/web, libs/common" > README.md && git add README.md && git commit -qm "docs: README" && git log --oneline -1
ID=$(gcloud builds submit --config=cloudbuild.yaml --region=us-east1 \
  --service-account=projects/$PROJECT_ID/serviceAccounts/mono-ci-1121@$PROJECT_ID.iam.gserviceaccount.com \
  --substitutions=_SHA=$(git rev-parse --short HEAD),_BASE=$(git rev-parse HEAD~1) . --async --format="value(id)" 2>/dev/null)
for i in $(seq 1 90); do gcloud builds describe $ID --region=us-east1 --format="value(status)" | grep -qE "SUCCESS|FAILURE|TIMEOUT|CANCELLED" && break; sleep 10; done; sleep 20
echo "build ${ID:0:8}: $(gcloud builds describe $ID --region=us-east1 --format='value(status)')"
gcloud builds log $ID --region=us-east1 2>/dev/null | grep -E "files changed|to build:|passed|built and|deployed|nothing|STATUS|SUCCESS|FAILURE" | sed -E 's/^Step #[0-9]+ - "[a-z-]+": //' | head -10
bb1ae11 docs: README
build 9afc47f9: FAILURE
files changed since 3692878: README.md

The script saw the change (README.md) and then the build failed. The bug is in changed.sh: when no file is under services/, grep '^services/' finds nothing and exits with code 1, and set -o pipefail (part of set -euo pipefail at the top) turns that into a failure of the whole script. "No match" is a normal answer here, not an error, but the shell cannot know that. Classic Bash surprise.

Fix it: "no match" is not an error

Why: { grep ... || true; } keeps grep's output but replaces its exit code 1 with success. The fix is itself a commit that changes only ci/, so this build also has nothing to deploy.

# Fix it: in changed.sh, grep finds no services/ line and exits 1, and "set -o pipefail" turns that into a failure. Allow "no match"
cd ~/repo
sed -i "s#echo \"\$FILES\" | grep '^services/' | cut#echo \"\$FILES\" | { grep '^services/' || true; } | cut#" ci/changed.sh && tail -1 ci/changed.sh
git commit -qam "ci: no changed service is not an error" && git log --oneline -1
ID=$(gcloud builds submit --config=cloudbuild.yaml --region=us-east1 \
  --service-account=projects/$PROJECT_ID/serviceAccounts/mono-ci-1121@$PROJECT_ID.iam.gserviceaccount.com \
  --substitutions=_SHA=$(git rev-parse --short HEAD),_BASE=$(git rev-parse HEAD~1) . --async --format="value(id)" 2>/dev/null)
for i in $(seq 1 90); do gcloud builds describe $ID --region=us-east1 --format="value(status)" | grep -qE "SUCCESS|FAILURE|TIMEOUT|CANCELLED" && break; sleep 10; done; sleep 20
echo "build ${ID:0:8}: $(gcloud builds describe $ID --region=us-east1 --format='value(status)')"
gcloud builds log $ID --region=us-east1 2>/dev/null | grep -E "files changed|to build:|passed|built and|deployed|nothing|STATUS|SUCCESS|FAILURE" | sed -E 's/^Step #[0-9]+ - "[a-z-]+": //' | head -10
echo "$FILES" | { grep '^services/' || true; } | cut -d/ -f2 | sort -u | tr '\n' ' '
7713f41 ci: no changed service is not an error
build 4059a591: SUCCESS
files changed since bb1ae11: ci/changed.sh
to build:
nothing changed in services or libs: nothing to deploy
Docs-only fixed
A README change failed the build because grep found nothing; with || true the pipeline finishes with nothing to deploy

Step 6: check the services, their images and the history

Why: each service answers with the commit that last built it, each image is tagged with the commit that built it, and the build history shows exactly which builds succeeded.

# Check both services, the image tags each one got, and the build history
for s in api web; do echo "mono-$s: $(curl -s $(gcloud run services describe mono-$s --region=us-east1 --format='value(status.url)'))"; done
for s in api web; do echo "$s images: $(gcloud artifacts docker tags list us-east1-docker.pkg.dev/$PROJECT_ID/mono/$s --format='value(tag)' | tr '\n' ' ')"; done
cd ~/repo && git log --oneline
gcloud builds list --region=us-east1 --filter="serviceAccount~mono-ci" --limit=7 --format="table(createTime.date('%H:%M:%S'):label=STARTED, status, substitutions._SHA:label=COMMIT)"
mono-api: {"commit":"3692878","service":"api (common lib 1.1)","version":"1.1.0"}
mono-web: <h1>Shop web 1.1.0</h1><p>web (common lib 1.1), commit 3692878</p>
Listing items under project your-project-id, location us-east1, repository mono.

api images: 0200dc3 3692878 6190f2a 
Listing items under project your-project-id, location us-east1, repository mono.

web images: 0200dc3 13e0eeb 3692878 
7713f41 ci: no changed service is not an error
bb1ae11 docs: README
3692878 common lib 1.1
13e0eeb web 1.1.0
6190f2a Upload .git to Cloud Build
906ebd6 api 1.1.0
0200dc3 Monorepo: api + web + common lib
STARTED   STATUS   COMMIT
23:20:29  SUCCESS  7713f41
23:19:29  FAILURE  bb1ae11
23:17:34  SUCCESS  3692878
23:15:39  SUCCESS  13e0eeb
23:13:45  SUCCESS  6190f2a
23:12:46  FAILURE  906ebd6
23:10:40  SUCCESS  0200dc3
  • mono-api and mono-web both run the shared library 1.1 from the same commit (the shared library change rebuilt both).
  • The image tags show the difference between the services: api was built for the first commit, the .gcloudignore fix and the library change; web for the first commit, its own change and the library change. Neither service was rebuilt for the other's changes, nor for the README.

POC: prove it works

  • The first build built and deployed both services.
  • Without .git in the build context, the diff failed; with a .gcloudignore, an api change built only api.
  • A web change built only web; a shared library change rebuilt both.
  • A README change first failed the pipeline (grep plus pipefail), and after the fix it built nothing.
  • Each service answers with the commit that built it, and the image tags prove nothing was rebuilt without a reason.

Quiz task: check what you learned

  1. A file under services/web/static/ changes. What is built? (Answer: only web.)
  2. Why must the Dockerfiles build from the repository root? (Answer: so they can copy libs/common, which is outside each service folder.)
  3. Why did the pipeline first fail with no Git history? (Answer: gcloud builds submit skips .git unless a .gcloudignore says otherwise.)
  4. In cloudbuild.yaml, what does $$s mean? (Answer: a shell variable $s; a single $ would be a Cloud Build substitution.)
  5. What should _BASE be in a real pipeline? (Answer: the last successfully deployed commit, stored somewhere (for example a tag or a file in a bucket), not just HEAD~1, so failed builds are not skipped.)

Clean up (delete everything)

# Clean up: services, images, pipeline account and its roles, and the build buckets
for s in api web; do gcloud run services delete mono-$s --region=us-east1 --quiet 2>&1 | tail -1; done
gcloud artifacts repositories delete mono --location=us-east1 --quiet 2>&1 | tail -1
for r in roles/run.admin roles/iam.serviceAccountUser roles/artifactregistry.writer roles/logging.logWriter roles/storage.objectViewer; do
  for t in 1 2 3 4 5; do gcloud projects remove-iam-policy-binding $PROJECT_ID --member="serviceAccount:mono-ci-1121@$PROJECT_ID.iam.gserviceaccount.com" --role=$r --condition=None --format="value(etag)" > /dev/null 2>&1 && break; sleep 5; done; done; echo "roles removed"
gcloud iam service-accounts delete mono-ci-1121@$PROJECT_ID.iam.gserviceaccount.com --quiet 2>&1 | tail -1
for b in $(gcloud storage buckets list --format="value(name)" | grep -E "cloudbuild"); do gcloud storage rm -r gs://$b --quiet > /dev/null 2>&1 && echo "deleted the build source bucket"; done
cd ~ && rm -rf repo monorepo probe.yaml
echo "Cloud Run services left: $(gcloud run services list --format='value(name)' | grep -c mono-)  repositories left: $(gcloud artifacts repositories list --format='value(name)' 2>/dev/null | grep -c mono)"
Deleted service [mono-api].
Deleted service [mono-web].
Deleted repository [mono].
roles removed
deleted service account [mono-ci-1121@your-project-id.iam.gserviceaccount.com]
deleted the build source bucket
Cloud Run services left: 0  repositories left: 0
Clean up
Both services, the image repository, the pipeline account and the build bucket deleted

FAQ

Why not one pipeline per service? With Git triggers you can: Cloud Build triggers have an included files filter (for example services/api/**), so a push only starts the matching service's trigger. The script approach shown here also works with manual and scheduled builds, and handles shared libraries in one place.

What do big monorepos use? Build tools that know the dependency graph: Bazel, Nx, Pants, Turborepo. They do what changed.sh does, but per file and with caching.

How do I connect this to GitHub? Create a Cloud Build trigger on your GitHub repository and pass the previous commit (Cloud Build gives $COMMIT_SHA; store the last deployed one). We did not test GitHub in this lab.

What about shared Kubernetes manifests or Terraform? Treat them like libs/: map each folder to what it affects, and keep that map in one script.

Does skipping builds risk stale services? Only if the rule is wrong. That is why shared code rebuilds everything, and why each service reports the commit it runs.

Why did the README change matter at all? Every pipeline must handle "nothing to do" as a success. Many teams find this bug the first time someone fixes a typo in the docs.

Troubleshooting

You seeWhyFix
Build step git fails at once.git was not uploadedAdd a .gcloudignore that does not list .git
detected dubious ownership in repositorygit inside the build container runs as another usergit config --global --add safe.directory /workspace (in our pipeline)
invalid value for 'build.substitutions'A shell $var in cloudbuild.yamlWrite $$var
A docs-only change failsgrep exits 1 with no match under pipefailTreat no match as success (the fix in this lab)
Every build rebuilds everything_BASE empty or too oldPass the last deployed commit as _BASE
Build fails with an empty logThe pipeline account's roles are not active yetWait (the probe loop), then build again

What next

Prepared by the CareerByteCode Team from our tested lab, with AI help for the write-up.

Read next →Database Migrations in CI/CD Without Downtime⏱ 14 min

⭐ Keep learning with CareerByteCode

Hand-picked to go deeper on this topic
News, guides and updates from the CareerByteCode team
🏅 Expert writer

The CareerByteCode team writes about what is new on careerbytecode.in: courses, labs, exams, events and the community, which is free to join for influencers and content creators in tech.

Get CareerByteCode Team's next blog by email

One email a week when they publish. Add the weekly Best of ByteBlog too. No spam, unsubscribe with one click.

We email you once to confirm. By subscribing you agree to the privacy policy.

💬 Comments (0)

Top · Newest

Sign in to comment

Related blogs

🧪⭐ Recommended by CareerByteCode · ByteLabs project bundleCloud and DevOps Realtime Starterโ‚น499Build the projects →