Skip to content

Instantly share code, notes, and snippets.

@christippett
Last active September 11, 2023 05:53
Show Gist options
  • Save christippett/54a1092c4f1f885081d5fce150239f09 to your computer and use it in GitHub Desktop.
Save christippett/54a1092c4f1f885081d5fce150239f09 to your computer and use it in GitHub Desktop.
Trigger Cloud Builds in a mono-repo
#!/bin/sh -e
IGNORE_FILES=$(ls -p | grep -v /)
TRACKING_BUCKET="$COMMIT_STATE_BUCKET/$REPO_NAME/$BRANCH_NAME"
detect_changed_folders() {
gsutil cp gs://$TRACKING_BUCKET/LAST_COMMIT . &> /dev/null || true
last_commit_sha=`cat LAST_COMMIT 2> /dev/null || git rev-list --max-parents=0 HEAD`
echo "Detecting changes from last build: $last_commit_sha"
folders=`git diff --name-only "$last_commit_sha" | sort -u | awk 'BEGIN {FS="/"} {print $1}' | uniq`
export changed_components=$folders
}
run_builds() {
echo ''
echo '__ __ _ _ _ _ '
echo '| \/ | | | (_) | | |'
echo '| \ / | ___ _ __ ___ ______| |__ _ _ _| | __| |'
echo '| |\/| |/ _ \| '_ \ / _ \______| '_ \| | | | | |/ _` |'
echo '| | | | (_) | | | | (_) | | |_) | |_| | | | (_| |'
echo '|_| |_|\___/|_| |_|\___/ |_.__/ \__,_|_|_|\__,_|'
echo ''
for component in $changed_components
do
if ! [[ " ${IGNORE_FILES[@]} " =~ "$component" ]]; then
echo -e "\nBuilding $component:"
if ! [[ -f "$component/cloudbuild.yaml" ]]; then
echo 'Skipping... cloudbuild.yaml not found'
else
(cd "$component" && gcloud builds submit --config=cloudbuild.yaml --async)
fi
fi
done
echo '-------------------------------------------------'
}
update_last_commit() {
echo "Updating latest build commit"
echo -n "$COMMIT_SHA" > LAST_COMMIT
gsutil cp LAST_COMMIT gs://$TRACKING_BUCKET/LAST_COMMIT
}
detect_changed_folders
run_builds
update_last_commit
steps:
- name: gcr.io/cloud-builders/git
args: ["fetch", "--unshallow"]
- name: gcr.io/cloud-builders/gcloud
entrypoint: bash
args: ["build.sh"]
env:
- "REPO_NAME=$REPO_NAME"
- "BRANCH_NAME=$BRANCH_NAME"
- "COMMIT_SHA=$COMMIT_SHA"
- "COMMIT_STATE_BUCKET=<bucket-name>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment