Cloud Build for CI/CD in GCP - Time & Space Complexity
When using Cloud Build for CI/CD, it's important to understand how build time grows as your project changes.
We want to know how the time to complete builds changes when the number of build steps or code size increases.
Analyze the time complexity of the following Cloud Build configuration.
steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/my-project/my-app', '.']
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/my-project/my-app']
images:
- 'gcr.io/my-project/my-app'
This config runs two main steps: building the container image and pushing it to the registry.
Look for repeated or costly operations in the build process.
- Primary operation: Building the container image involves processing all source files.
- How many times: Once per build, but the time depends on the number of files and build steps.
As the number of source files or build steps increases, the build time grows roughly in proportion.
| Input Size (number of files or steps) | Approx. Operations (build time) |
|---|---|
| 10 | Short build time, quick processing |
| 100 | Longer build time, more processing |
| 1000 | Much longer build time, heavy processing |
Pattern observation: Build time grows roughly linearly with the amount of work to do.
Time Complexity: O(n)
This means build time increases roughly in direct proportion to the size of the input (files or steps).
[X] Wrong: "Adding more build steps won't affect build time much because they run fast."
[OK] Correct: Each build step adds time, so more steps usually mean longer builds.
Understanding how build time scales helps you design efficient CI/CD pipelines and shows you can think about real-world system performance.
"What if we added parallel build steps? How would that change the time complexity?"
Practice
Solution
Step 1: Understand Cloud Build's role
Cloud Build automates the process of building, testing, and deploying code, which is essential in CI/CD pipelines.Step 2: Eliminate unrelated options
Storing data, monitoring network traffic, and managing permissions are not primary functions of Cloud Build.Final Answer:
To automate building, testing, and deploying code -> Option BQuick Check:
Cloud Build = Automate build/test/deploy [OK]
- Confusing Cloud Build with storage services
- Thinking Cloud Build manages network or permissions
- Assuming Cloud Build only builds code without testing or deploying
Solution
Step 1: Review Cloud Build YAML syntax
The correct key for build steps is 'steps', and each step uses 'name' for the builder image and 'args' for commands.Step 2: Compare options to syntax
steps: - name: 'gcr.io/cloud-builders/docker' args: ['build', '-t', 'gcr.io/my-project/my-image', '.'] matches the correct syntax. Other options use incorrect keys like 'build_steps', 'actions', or 'commands'.Final Answer:
steps: - name: 'gcr.io/cloud-builders/docker' args: ['build', '-t', 'gcr.io/my-project/my-image', '.'] -> Option CQuick Check:
Use 'steps' with 'name' and 'args' in Cloud Build YAML [OK]
- Using wrong keys like 'build_steps' or 'actions'
- Confusing 'args' with 'commands'
- Omitting the builder image name
steps: - name: 'ubuntu' args: ['echo', 'Hello, Cloud Build!']
Solution
Step 1: Analyze the build step
The step uses the 'ubuntu' image and runs the command 'echo Hello, Cloud Build!'. This command prints the message to the output.Step 2: Determine expected output
Since 'echo' prints text, the output will be 'Hello, Cloud Build!'. There is no syntax error or missing image because 'ubuntu' is a valid public image.Final Answer:
Hello, Cloud Build! -> Option DQuick Check:
Echo command prints text output [OK]
- Assuming build fails due to image name
- Thinking no output is produced by echo
- Confusing YAML syntax errors with valid syntax
steps: - name: 'gcr.io/cloud-builders/docker' args: ['build', '-t', 'gcr.io/my-project/my-image']
Solution
Step 1: Check Docker build command syntax
The Docker build command requires a build context, usually '.' at the end, to specify where to find the Dockerfile.Step 2: Identify missing argument
The args list misses the '.' context argument, causing the build to fail immediately.Final Answer:
Missing the build context (dot) at the end of args -> Option AQuick Check:
Docker build needs context path '.' [OK]
- Assuming image name is wrong
- Thinking YAML indentation is the issue
- Using string instead of list for args
Solution
Step 1: Understand step dependencies in Cloud Build
Cloud Build starts steps in parallel by default. Use 'waitFor: ["-"]' to make a step wait for all previous steps to succeed.Step 2: Analyze options for correct workflow
steps: - name: 'gcr.io/cloud-builders/docker' args: ['build', '-t', 'gcr.io/my-project/my-image', '.'] - name: 'gcr.io/cloud-builders/docker' args: ['run', 'my-image', 'test'] waitFor: ['-'] - name: 'gcr.io/cloud-builders/gcloud' args: ['app', 'deploy'] waitFor: ['-'] in steps: - name: 'gcr.io/cloud-builders/docker' args: ['build', '-t', 'gcr.io/my-project/my-image', '.'] - name: 'gcr.io/cloud-builders/docker' args: ['run', 'my-image', 'test'] waitFor: ['-'] - name: 'gcr.io/cloud-builders/gcloud' args: ['app', 'deploy'] waitFor: ['-'] ensures tests wait for build, and deploy waits for both.Final Answer:
steps: - name: 'gcr.io/cloud-builders/docker' args: ['build', '-t', 'gcr.io/my-project/my-image', '.'] - name: 'gcr.io/cloud-builders/docker' args: ['run', 'my-image', 'test'] waitFor: ['-'] - name: 'gcr.io/cloud-builders/gcloud' args: ['app', 'deploy'] waitFor: ['-'] -> Option AQuick Check:
waitFor ['-'] on test and deploy enforces build -> test -> deploy [OK]
- Adding 'waitFor' incorrectly on all steps
- Omitting 'waitFor' causing deploy to run too early
- Misplacing 'waitFor' inside wrong steps
Ready to prove it?
Pass the test and this pattern moves unknown
