-
Notifications
You must be signed in to change notification settings - Fork 0
Improve Cloud Build module for parallel builds and multi-stage Docker caching #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
cfadc16
501b248
e0b88a6
c08439f
125f34b
82adfc7
59f2183
cbfc2bc
d539f4b
6621f7e
1fc9a22
1683f6e
1979b51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,48 @@ | ||
| # Cloud Build configuration with branch-based caching. | ||
| # Enables caching by pulling the previous image with a given "cache tag", if it exists, and reusing its layers. | ||
| # Cloud Build configuration with BuildKit inline cache for multi-stage builds. | ||
| # BuildKit is required to properly cache intermediate stages in multi-stage builds. | ||
| steps: | ||
| - name: 'gcr.io/cloud-builders/docker' | ||
| id: Pull previous image | ||
| id: Pull cache images | ||
| entrypoint: bash | ||
| args: | ||
| - -c | ||
| - | | ||
| echo "Attempting to pull cache image: $_IMAGE_NAME:$_CACHE_TAG" | ||
| docker pull "$_IMAGE_NAME:$_CACHE_TAG" || echo "Cache image not found, will build without cache" | ||
| echo "Attempting to pull cache image..." | ||
| # With BuildKit inline cache, we only need to pull the final image | ||
| # which contains metadata about all intermediate stages | ||
| docker pull "$_IMAGE_NAME:$_CACHE_TAG" || echo "Cache image not found, building from scratch" | ||
| - name: 'gcr.io/cloud-builders/docker' | ||
| id: Build image | ||
| id: Build image with BuildKit | ||
| entrypoint: bash | ||
| env: | ||
| - 'DOCKER_BUILDKIT=1' | ||
| args: | ||
| - -c | ||
| - | | ||
| if docker image inspect "$_IMAGE_NAME:$_CACHE_TAG" > /dev/null 2>&1; then | ||
| echo "Using cache from $_IMAGE_NAME:$_CACHE_TAG" | ||
| docker build --tag="$_IMAGE_TAG" --cache-from="$_IMAGE_NAME:$_CACHE_TAG" --build-arg BASE_IMAGE="$_BASE_DIGEST" . | ||
| else | ||
| echo "No cache found for $_IMAGE_NAME:$_CACHE_TAG, building without --cache-from" | ||
| docker build --tag="$_IMAGE_TAG" --build-arg BASE_IMAGE="$_BASE_DIGEST" . | ||
| fi | ||
| echo "Building with BuildKit and inline cache..." | ||
| # Build with BuildKit inline cache | ||
| # --cache-from pulls cache metadata from previous build | ||
| # --build-arg BUILDKIT_INLINE_CACHE=1 embeds cache metadata in the image | ||
| docker build \ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this work if the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current code should work fine without the conditional. Current approach (works correctly):
Docker/BuildKit behavior:
|
||
| --tag="$_IMAGE_TAG" \ | ||
| --cache-from="$_IMAGE_NAME:$_CACHE_TAG" \ | ||
| --build-arg BUILDKIT_INLINE_CACHE=1 \ | ||
| --build-arg BASE_IMAGE="$_BASE_DIGEST" \ | ||
| . | ||
| - name: 'gcr.io/cloud-builders/docker' | ||
| id: Tag cache | ||
| id: Tag cache image | ||
| entrypoint: bash | ||
| args: ['-c', 'docker tag "$_IMAGE_TAG" "$_IMAGE_NAME:$_CACHE_TAG"'] | ||
| waitFor: ['Build image'] | ||
| waitFor: ['Build image with BuildKit'] | ||
|
|
||
| - name: 'gcr.io/cloud-builders/docker' | ||
| id: Push cache | ||
| id: Push cache image | ||
| entrypoint: bash | ||
| args: ['-c', 'docker push "$_IMAGE_NAME:$_CACHE_TAG"'] | ||
| waitFor: ['Tag cache'] | ||
| waitFor: ['Tag cache image'] | ||
|
|
||
| images: | ||
| - '$_IMAGE_TAG' | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you see my previous comment about just passing the timeout flag rather than trying to implement the polling yourself? Request Changes for that. Rest is looking good!