Skip to content

Professional Cloud Developer · Domain 2: Building and testing applications · 23% of the exam

Task 2.2: Building.

Building and storing containers from source with Cloud Build and Artifact Registry, and build provenance that Binary Authorization can check.

Study it

Sample questions

Try each one before opening the answer. Every option is explained, with the Google Cloud documentation page that proves it.

Question 1 · choose 1

A build fails in its second step with "No such file: /tmp/version.txt": steps: - id: stamp name: ubuntu entrypoint: bash args: ['-c', 'git describe --tags > /tmp/version.txt'] - id: build name: gcr.io/cloud-builders/docker entrypoint: bash args: ['-c', 'docker build --build-arg VERSION=$(cat /tmp/version.txt) -t "$_IMAGE" .'] What should the team change?

  1. AAdd waitFor: ['stamp'] to the build step
  2. BRun both steps with the same builder image, ubuntu, so that they share one container
  3. CSet the step's dir field to /tmp in both steps
  4. DWrite the file to /workspace/version.txt and read it from there
Show the answer and why
  • AAdd waitFor: ['stamp'] to the build step

    Incorrect

    The order isn't the problem: a file outside a volume is gone when the first step's container is discarded.

  • BRun both steps with the same builder image, ubuntu, so that they share one container

    Incorrect

    Each step runs in its own container that is discarded afterwards, whatever the image.

  • CSet the step's dir field to /tmp in both steps

    Incorrect

    Files in /tmp still disappear with the first container.

  • DWrite the file to /workspace/version.txt and read it from there

    Correct

    Anything written to /workspace, the default volume, is available to later steps.

Cloud Build runs each build step in an isolated container that is discarded after the step. Volumes keep their contents for the whole build: /workspace is the default volume, into which the source is extracted, and user-defined volumes can be attached to steps. Files written to /tmp in one step are not available to the next.

Question 2 · choose 1

To speed up a build, an engineer added waitFor: ['-'] to the test step: steps: - id: codegen name: gcr.io/cloud-builders/npm args: ['run', 'generate'] # writes /workspace/src/api.gen.ts - id: test name: gcr.io/cloud-builders/npm args: ['test'] waitFor: ['-'] - id: lint name: gcr.io/cloud-builders/npm args: ['run', 'lint'] waitFor: ['-'] Since then, the test step fails now and then because src/api.gen.ts is missing or incomplete. Which change fixes it and keeps lint running in parallel?

  1. ASet waitFor: ['codegen'] on the test step
  2. BMove the test step above the codegen step in the file
  3. CWrite the generated file to a user-defined volume instead of /workspace
  4. DGive the test step its own timeout of 10 minutes
Show the answer and why
  • ASet waitFor: ['codegen'] on the test step

    Correct

    The test step then starts only after codegen completes, while lint still starts at once.

  • BMove the test step above the codegen step in the file

    Incorrect

    With waitFor: ['-'], the test step starts at the beginning of the build wherever it is listed.

  • CWrite the generated file to a user-defined volume instead of /workspace

    Incorrect

    Either location is shared; the test step still starts before codegen has written the file.

  • DGive the test step its own timeout of 10 minutes

    Incorrect

    A timeout limits how long the step runs; it doesn't make it wait for codegen.

The waitFor field controls when a step starts. waitFor: ['-'] makes a step run immediately when the build starts, in parallel with others, and waitFor with step IDs makes it wait until those steps have completed. Because /workspace is shared, the test step must wait for the codegen step that writes the file it reads.

Question 3 · choose 1

A repository has no .gcloudignore file. Its .gitignore lists config/generated.json, a file that a local script creates and that the Dockerfile copies into the image. A developer runs gcloud builds submit from the repository root, and the docker build step fails because config/generated.json isn't found. What should the developer do?

  1. APass the file's path in the --substitutions flag of gcloud builds submit
  2. BRun the build with --no-source so that gcloud uploads the directory as it is
  3. CAdd a .gcloudignore file that excludes what should be skipped but not config/generated.json
  4. DCommit config/generated.json to Git but keep it listed in .gitignore
Show the answer and why
  • APass the file's path in the --substitutions flag of gcloud builds submit

    Incorrect

    Substitutions replace variables in the build config; they don't add files to the uploaded source.

  • BRun the build with --no-source so that gcloud uploads the directory as it is

    Incorrect

    --no-source is for builds that have no source to pass in, so nothing is uploaded.

  • CAdd a .gcloudignore file that excludes what should be skipped but not config/generated.json

    Correct

    Without a .gcloudignore file, gcloud generates one that respects the .gitignore entries, so the file is never uploaded.

  • DCommit config/generated.json to Git but keep it listed in .gitignore

    Incorrect

    gcloud builds submit uploads the local directory and still applies the .gitignore-based rules to it.

gcloud builds submit uploads the files in the specified directory. If a .gcloudignore file exists in the top-level directory, the files it lists are ignored; if there is none but a .gitignore exists, the gcloud CLI generates a Git-compatible .gcloudignore that respects the ignored files. A file listed in .gitignore therefore never reaches the build unless an explicit .gcloudignore says otherwise.

Question 4 · choose 1

On a build server, ~/.docker/config.json contains: {"credHelpers": {"europe-docker.pkg.dev": "gcloud"}, "auths": {"europe-docker.pkg.dev": {"auth": "b2F1dGgy..."}}} The gcloud CLI there is signed in as a user without write access. A release script logs in to europe-docker.pkg.dev with docker login, using an access token of the release service account, which has Artifact Registry Writer. The push is still denied. What explains it?

  1. AAccess tokens are refused by Artifact Registry for Docker pushes
  2. BDocker uses the credential helper for that host first, so the token in auths is ignored
  3. CArtifact Registry Writer allows pulls only, so a push needs the Repository Administrator role
  4. Ddocker login stores the token in credStore, which Docker reads last
Show the answer and why
  • AAccess tokens are refused by Artifact Registry for Docker pushes

    Incorrect

    A short-lived service account access token is a documented password option for Docker.

  • BDocker uses the credential helper for that host first, so the token in auths is ignored

    Correct

    Docker checks for a credential helper for the host first; with both sections present, auths is ignored.

  • CArtifact Registry Writer allows pulls only, so a push needs the Repository Administrator role

    Incorrect

    The role isn't the issue: the push is made as the gcloud user, not as the release service account.

  • Ddocker login stores the token in credStore, which Docker reads last

    Incorrect

    A token used as a password is stored in the auths section, which is ignored here because of credHelpers.

Docker keeps authentication settings in config.json: credential helper settings in credHelpers, and passwords such as an access token in auths. When Docker connects to a registry, it first checks for a credential helper associated with that host, so if config.json has Artifact Registry settings in both sections, the auths settings are ignored. Here the gcloud helper authenticates as the gcloud CLI's signed-in user.

Question 5 · choose 1

This build config tags images correctly when a trigger runs it, but a manual gcloud builds submit run tags the image with the unexpanded text instead of the repository path: steps: - name: gcr.io/cloud-builders/docker args: ['build', '-t', '${_IMAGE_NAME}', '.'] substitutions: _LOCATION: europe-west1 _REPOSITORY: apps _IMAGE_NAME: '${_LOCATION}-docker.pkg.dev/${PROJECT_ID}/${_REPOSITORY}/web' What should the team add so that manual builds behave like trigger builds?

  1. Aoptions: substitutionOption: 'ALLOW_LOOSE'
  2. B$$ in front of each reference inside _IMAGE_NAME
  3. Coptions: dynamicSubstitutions: true
  4. D--substitutions=_LOCATION=europe-west1,_REPOSITORY=apps on the command
Show the answer and why
  • Aoptions: substitutionOption: 'ALLOW_LOOSE'

    Incorrect

    ALLOW_LOOSE only skips the check for missing substitutions; it doesn't expand references inside one.

  • B$$ in front of each reference inside _IMAGE_NAME

    Incorrect

    $$ produces a literal $, so the references would never be expanded.

  • Coptions: dynamicSubstitutions: true

    Correct

    Triggers always set dynamic substitutions; manual builds need it set in the config for one substitution to reference another.

  • D--substitutions=_LOCATION=europe-west1,_REPOSITORY=apps on the command

    Incorrect

    The values are already defined; the reference inside _IMAGE_NAME is what isn't expanded.

A user-defined substitution can reference other variables only when the dynamicSubstitutions option is true. For builds invoked by a trigger, the field is always true; for manual builds, it must be set in the build config file, as the page's example with _IMAGE_NAME referencing ${PROJECT_ID} shows. ALLOW_LOOSE and $$ escaping solve different problems.

Question 6 · choose 1

A Python service has no Dockerfile. The team wants Cloud Build to build a container image for it straight from source and store it in Artifact Registry. Which command does that?

  1. Agcloud builds submit --tag=europe-docker.pkg.dev/acme/apps/api
  2. Bgcloud builds submit --pack image=europe-docker.pkg.dev/acme/apps/api
  3. Cgcloud builds submit --no-source
  4. Dgcloud builds submit --config=cloudbuild.yaml with an empty steps list
Show the answer and why
  • Agcloud builds submit --tag=europe-docker.pkg.dev/acme/apps/api

    Incorrect

    Building with --tag uses a Dockerfile, which this service doesn't have.

  • Bgcloud builds submit --pack image=europe-docker.pkg.dev/acme/apps/api

    Correct

    --pack builds the image with Google Cloud's buildpacks, which need no Dockerfile.

  • Cgcloud builds submit --no-source

    Incorrect

    --no-source uploads no source at all, so there is nothing to build.

  • Dgcloud builds submit --config=cloudbuild.yaml with an empty steps list

    Incorrect

    A config without steps runs nothing, so no image is built.

Cloud Build can build container images with a Dockerfile or with Google Cloud's buildpacks. To build with buildpacks, run gcloud builds submit --pack with the image name, and optionally a builder such as gcr.io/buildpacks/builder.

Question 7 · choose 1

A build config lists two images, but its only build step builds one of them: steps: - name: gcr.io/cloud-builders/docker args: ['build', '-t', 'europe-docker.pkg.dev/acme/apps/web', '.'] images: - europe-docker.pkg.dev/acme/apps/web - europe-docker.pkg.dev/acme/apps/worker What happens when the build runs?

  1. ACloud Build pushes web and skips worker with a warning
  2. BCloud Build builds worker from the same Dockerfile and pushes both
  3. CThe build fails, because worker isn't produced
  4. DCloud Build pushes web and creates an empty worker image
Show the answer and why
  • ACloud Build pushes web and skips worker with a warning

    Incorrect

    A missing image isn't skipped; it makes the build fail.

  • BCloud Build builds worker from the same Dockerfile and pushes both

    Incorrect

    The images field names images to push; it doesn't build anything.

  • CThe build fails, because worker isn't produced

    Correct

    If a specified image is not produced during the build, the build fails.

  • DCloud Build pushes web and creates an empty worker image

    Incorrect

    Cloud Build doesn't create images that no step built.

The images field lists Docker images that Cloud Build pushes to Artifact Registry. Images that a build creates but doesn't list are discarded at the end of the build, and if a listed image is not produced during the build, the build fails.

Question 8 · choose 1

A Cloud Build pipeline builds a container image into Artifact Registry and also uploads a command-line binary to a Cloud Storage bucket for download. The team turned on build provenance and expects provenance for both outputs. What will it get?

  1. AProvenance for the container image in Artifact Registry only
  2. BProvenance for both, stored next to each output
  3. CProvenance for the binary only, because images need a signature first
  4. DNo provenance for either output until a private pool is used
Show the answer and why
  • AProvenance for the container image in Artifact Registry only

    Correct

    Cloud Build generates build provenance only for artifacts stored in Artifact Registry.

  • BProvenance for both, stored next to each output

    Incorrect

    An object in a Cloud Storage bucket isn't stored in Artifact Registry, so it gets none.

  • CProvenance for the binary only, because images need a signature first

    Incorrect

    Images in Artifact Registry are exactly what provenance is generated for.

  • DNo provenance for either output until a private pool is used

    Incorrect

    The only documented limitation is the Artifact Registry one.

Build provenance is verifiable data about a build. A documented limitation is that Cloud Build only generates build provenance for artifacts stored in Artifact Registry, so a file placed in a Cloud Storage bucket gets none.

Practice domain 2 →Practice all domains →