GCP: Cloud Run preview build fails because of a missing tag "latest"

Viewed 302

If have an issue where cloud build is failing on creating a preview build for use in github pull requests.

I have

  • a github organization with the cloud build app installed.
  • a cloud build set-up with triggers to deploy to cloud run
  • functional build on master deploy (doesn't really matter here).

The following is my cloudbuild-preview.yaml file. The failing step is the last one: "link revision on pull request"

steps:
  - id: "build image"
    name: "gcr.io/cloud-builders/docker"
    args:
      [
        "build",
        "-t",
        "$_GCR_HOSTNAME/${PROJECT_ID}/${_SERVICE_NAME}:${_PR_NUMBER}-${SHORT_SHA}",
        ".",
      ]

  - id: "push image"
    name: "gcr.io/cloud-builders/docker"
    args:
      [
        "push",
        "$_GCR_HOSTNAME/${PROJECT_ID}/${_SERVICE_NAME}:${_PR_NUMBER}-${SHORT_SHA}",
      ]

  - id: "deploy revision with tag"
    name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
    entrypoint: "gcloud"
    args:
      [
        "beta",
        "run",
        "deploy",
        "${_SERVICE_NAME}",
        "--platform",
        "managed",
        "--region",
        "${_REGION}",
        "--image",
        "$_GCR_HOSTNAME/${PROJECT_ID}/${_SERVICE_NAME}:${_PR_NUMBER}-${SHORT_SHA}",
        "--tag",
        "pr-${_PR_NUMBER}",
        "--no-traffic",
      ]

  - id: "link revision on pull request"
    name: "$_GCR_HOSTNAME/${PROJECT_ID}/deployment-previews" # our custom builder
    args:
      [
        "set",
        "--project-id",
        "${PROJECT_ID}",
        "--region",
        "${_REGION}",
        "--service",
        "${_SERVICE_NAME}",
        "--pull-request",
        "${_PR_NUMBER}",
        "--repo-name",
        "${_GITHUB_REPO}",
        "--commit-sha",
        "${SHORT_SHA}",
      ]
timeout: 1400s
options:
  machineType: N1_HIGHCPU_8
substitutions:
  _GCR_HOSTNAME: eu.gcr.io
  _SERVICE_NAME: redacted-service
  _REGION: europe-west4
  _GITHUB_REPO: $(pull_request.pull_request.head.repo.full_name)

The execution fails with

Step #3 - "link revision on pull request": Error response from daemon: manifest for eu.gcr.io/redacted-org/deployment-previews:latest not found: manifest unknown: Failed to fetch "latest" from request "/v2/redacted-org/deployment-previews/manifests/latest".
Step #3 - "link revision on pull request": Using default tag: latest
Step #3 - "link revision on pull request": Pulling image: eu.gcr.io/redacted-org/deployment-previews
Starting Step #3 - "link revision on pull request"

What I don't undestand is why the sep is even looking for a :latest tag. There is none. The above steps don't create one. The container registry does not contain one. How to tell that build step to use the proper image tagged with ${_PR_NUMBER}-${SHORT_SHA}? Where can I dive into the magic here? Where is the definition of this magic build step?!

Thank you very much for any ideas.

1 Answers

When you don't specify an image tag, tools will always try to pull the :latest image. In Cloud Build, you can specify a specific version of a builder image by simply including the tag in the name for your build step:

  - id: "link revision on pull request"
    name: "$_GCR_HOSTNAME/${PROJECT_ID}/deployment-previews:${_PR_NUMBER}-${SHORT_SHA}" # our custom builder
    args:
      [
        "set",
        "--project-id",
        "${PROJECT_ID}",
        "--region",
        "${_REGION}",
        "--service",
        "${_SERVICE_NAME}",
        "--pull-request",
        "${_PR_NUMBER}",
        "--repo-name",
        "${_GITHUB_REPO}",
        "--commit-sha",
        "${SHORT_SHA}",
      ]
Related