You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add cache fallback to "latest" for cloud-build-docker module (#12)
## Summary:
No one likes slow PR checks. With the current cloud-build-docker implementation, caching is based on the branch name. This means the first docker build for a given branch will take longer because it won't have a cache to pull from.
This change enhances the cloud-build-docker module to automatically fall back to using "latest" as the cache tag when the specified cache tag doesn't exist in the container registry. This improves build performance by ensuring builds can still benefit from layer caching even when branch-specific caches are not available.
Key changes:
- Added `check_cache_tag_exists()` function to verify if a cache tag exists
- Added `get_effective_cache_tag()` function that determines the effective cache tag with fallback logic
- Modified `build_image()` to use the effective cache tag instead of always using the provided tag
- Enhanced Cloud Build configuration with better logging and error handling
- Updated documentation to reflect the new cache fallback behavior
The implementation provides clear logging to indicate when fallback occurs, making it easy to understand the caching behavior during builds.
## Test plan:
This was tested [here](Khan/internal-services#355)
Author: jwbron
Reviewers: csilvers
Required Reviewers:
Approved By: csilvers
Checks: ✅ 1 check was successful
Pull Request URL: #12
Copy file name to clipboardExpand all lines: terraform/modules/cloud-build-docker/README.md
+8-4Lines changed: 8 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,7 @@ A reusable Terraform module for building Docker images using Google Cloud Build
6
6
7
7
-**Cloud Build Integration**: Uses Google Cloud Build for reliable, scalable Docker image building
8
8
-**Branch-based Caching**: Optimizes build times by caching layers based on branch names
9
+
-**Cache Fallback**: Automatically falls back to "latest" tag if the specified cache tag doesn't exist, ensuring we always have some level of caching
9
10
-**Digest Tracking**: Returns full image digests for precise versioning in Terraform
10
11
-**Flexible Dockerfile Support**: Supports custom Dockerfile names and locations
11
12
-**Build Arguments**: Supports custom build arguments and base image digests
@@ -132,13 +133,15 @@ module "secure_app" {
132
133
133
134
### Build Process
134
135
1.**Context Preparation**: The module prepares the build context and handles Dockerfile symlinks if needed
135
-
2.**Cloud Build Submission**: Submits the build to Google Cloud Build with appropriate substitutions
136
-
3.**Caching**: Uses branch-based caching to optimize build times
137
-
4.**Digest Retrieval**: Queries the built image to get its full digest
138
-
5.**Output**: Returns the digest for use in other Terraform resources
136
+
2.**Cache Tag Resolution**: Determines the effective cache tag by checking if the specified tag exists, falling back to "latest" if not
137
+
3.**Cloud Build Submission**: Submits the build to Google Cloud Build with appropriate substitutions
138
+
4.**Caching**: Uses the effective cache tag for layer caching to optimize build times
139
+
5.**Digest Retrieval**: Queries the built image to get its full digest
140
+
6.**Output**: Returns the digest for use in other Terraform resources
139
141
140
142
### Caching Strategy
141
143
-**Branch-based**: Uses the `image_tag_suffix` as a cache tag
144
+
-**Fallback to Latest**: If the specified cache tag doesn't exist, falls back to using "latest" as the cache tag, ensuring we always have some level of caching
142
145
-**Layer Reuse**: Subsequent builds reuse cached layers when possible
143
146
-**Cache Invalidation**: Cache is automatically invalidated when Dockerfile or context changes
0 commit comments