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

✍️ 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

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 pipelineThe 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 under | Build |
|---|---|
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
.gcloudignorefile 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_ONLYEach service's Dockerfile builds from the repository root, so it can copy the shared library: docker build -f services/api/Dockerfile ..
Before you start
- A Google Cloud account (the free trial works) and a project.
- 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- Create the folder
~/monorepowith these files:ci/changed.shandcloudbuild.yamlfrom 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:appPart 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 | sort0200dc3 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.pyStep 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 triesStep 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 -12build 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
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 -6906ebd6 api 1.1.0
build c11d68ac: FAILURE
ERROR: build step 0 "gcr.io/cloud-builders/git" failed: step exited with non-zero status: 1The 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
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 -1013e0eeb web 1.1.0
build dcbbcead: SUCCESS
built and pushed web
deployed mono-webOnly 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 -103692878 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
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 -10bb1ae11 docs: README
build 9afc47f9: FAILURE
files changed since 3692878: README.mdThe 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 -10echo "$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
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 0200dc3mono-apiandmono-webboth 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
.gcloudignorefix 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
.gitin 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
- A file under
services/web/static/changes. What is built? (Answer: only web.) - Why must the Dockerfiles build from the repository root? (Answer: so they can copy
libs/common, which is outside each service folder.) - Why did the pipeline first fail with no Git history? (Answer:
gcloud builds submitskips.gitunless a.gcloudignoresays otherwise.) - In
cloudbuild.yaml, what does$$smean? (Answer: a shell variable$s; a single$would be a Cloud Build substitution.) - What should
_BASEbe in a real pipeline? (Answer: the last successfully deployed commit, stored somewhere (for example a tag or a file in a bucket), not justHEAD~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
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 see | Why | Fix |
|---|---|---|
Build step git fails at once | .git was not uploaded | Add a .gcloudignore that does not list .git |
detected dubious ownership in repository | git inside the build container runs as another user | git config --global --add safe.directory /workspace (in our pipeline) |
invalid value for 'build.substitutions' | A shell $var in cloudbuild.yaml | Write $$var |
| A docs-only change fails | grep exits 1 with no match under pipefail | Treat no match as success (the fix in this lab) |
| Every build rebuilds everything | _BASE empty or too old | Pass the last deployed commit as _BASE |
| Build fails with an empty log | The pipeline account's roles are not active yet | Wait (the probe loop), then build again |
What next
- CI/CD basics first: CI/CD for Beginners: Build and Deploy on Every Git Push.
- More Cloud Build: Cloud Build Hands-On: Pipelines Without Managing Servers.
- Git skills behind the diff: Git for DevOps: Branches, Merges and Fixing Mistakes.
- More hands-on posts: ByteBlog. Practice in ByteLabs and see the courses.
Prepared by the CareerByteCode Team from our tested lab, with AI help for the write-up.
⭐ Keep learning with CareerByteCode
Hand-picked to go deeper on this topicGet 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.
Related blogs
CareerByteCode TeamDatabase Migrations in CI/CD Without Downtime
What you build: an API under constant load, a column rename that breaks every request, then the same change with zero errors using expand and contract. Every step tested.
#beginners
CareerByteCode TeamCI/CD for Beginners: Build and Deploy on Every Git Push
What you build: a pipeline from a git commit to a live Cloud Run app, with a test address, one-command promote and rollback, in your own Google Cloud account. Every step tested.
#beginners
CareerByteCode TeamCloud Build Hands-On: Pipelines Without Managing Servers
What you build: a build, test and push pipeline in Cloud Build, in your own Google Cloud account. Every step, command and screenshot explained for beginners.
#cicd
CareerByteCode TeamHelm Chart CI/CD: Lint, Test and Publish Charts Automatically
What you build: a Helm chart pipeline: lint, kubeconform, helm-unittest, then publish to Artifact Registry with immutable versions, plus a real failing test, a refused overwrite, and install/upgrade/r
💬 Comments (0)
Top · NewestSign in to comment