Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

cloud build doesn't recognize build directory for docker images to artifact registry

Hi Team,

cloud build doesn't recognize build directory for docker images to the artifact registry, whily triggering job its throwing error as 

tep #2: Successfully built 42d23e382d82
Step #2: Successfully tagged asia-south1-docker.pkg.dev/gcp/csd-icp-product:e442b278-a43d-4eb3-b3e8-5550d45691f8
Finished Step #2
Starting Step #3
Step #3: Already have image (with digest): gcr.io/cloud-builders/docker
Step #3: The push refers to repository [asia-south1-docker.pkg.dev/gcpnagement/csd-icp-product/663638a1-fa3a-41ed-b51e-80eb88ffb6f6]
Step #3: An image does not exist locally with the tag: asia-south1-docker.pkg.dev/gcpfleetmanagement/csd-icp-product/663638a1-fa3a-41ed-b51e-80eb88ffb6f6
Finished Step #3
ERROR
ERROR: build step 3 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 1

 any idea what is causing this isssue, here's full script cloudbuild.yaml

steps:
  #- name: 'maven:3.6.3-jdk-8'
   # entrypoint: 'mvn'
    dir: '/workspace/cicd-icp/csd-icp-product-main-icp-api-server-develop/icp-api-server-develop'
    args: ['clean', 'install']

 

    args: ['auth', 'configure-docker', 'asia-south1-docker.pkg.dev']

 

    dir: '/workspace/cicd-icp/csd-icp-product-main-icp-api-server-develop/icp-api-server-develop'
    args: ['build', '-t', 'asia-south1-docker.pkg.dev/$PROJECT_ID/csd-icp-product:$BUILD_ID', '-f', 'Dockerfile', '.']

 

 

    env:
      - 'KUBECONFIG=/root/.kube/config'
    args: ['config', 'get-contexts']

 

    env:
      - 'KUBECONFIG=/root/.kube/config'
    args: ['config', 'set-context', '--current', '--namespace=csd-icp']

 

    dir: 'csd-icp-products/icp-api-server-develop'
    args: ['upgrade', 'csd', '--install', 'icp-chart', '--set', 'image.tag=$BUILD_ID']

 

logsBucket: gs://cloudbuild-bucket

 

options:
  defaultLogsBucketBehavior: REGIONAL_USER_OWNED_BUCKET
0 1 287
1 REPLY 1

Hello avinashsr,

Welcome to Google Cloud Community!

The error message indicates that Cloud Build is unable to find the built image locally before attempting to push it to the Artifact Registry. This mismatch is causing the push to fail. Here's a breakdown of the issue and potential solutions based on your cloudbuild.yaml script:

Problem: Cloud Build builds the image but cannot find it with the specific tag you're trying to push in step 3.

Possible Reasons:

  1. Incorrect Tag Name: The tag name in your docker push command (asia-south1-docker.pkg.dev/$PROJECT_ID/csd-icp-product/663638a1-fa3a-41ed-b51e-80eb88ffb6f6:$BUILD_ID) might not match the actual tag generated during the build process.
  2. Missing Context: The docker build command (docker build -t asia-south1-docker.pkg.dev/$PROJECT_ID/csd-icp-product:$BUILD_ID ...) might not be including the context directory (/workspace/cicd-icp/csd-icp-product-main-icp-api-server-develop/icp-api-server-develop) where the Dockerfile resides.

Solutions:

  1. Verify Tag Naming: Double-check the tag name generated during the build process. You can potentially use the gcr.io/cloud-builders/docker image with the inspect command to examine the built image and its tags after step 3.
  2. Set Build Context: Ensure the docker build command includes the context directory where the Dockerfile is located. Here's the corrected line:

YAML:

 

 

- name: 'gcr.io/cloud-builders/docker'
  dir: '/workspace/cicd-icp/csd-icp-product-main/icp-api-server-develop/icp-api-server-develop'
  args: ['build', '-t', 'asia-south1-docker.pkg.dev/$PROJECT_ID/csd-icp-product:$BUILD_ID', '-f', 'Dockerfile', '.']

 

 

Additional Tips:

  • You can utilize Cloud Build logs to inspect the output of each step and identify any errors during the build or tagging process.
  • Consider using a multi-stage build process in your Dockerfile. This can help optimize the final image size by separating the build and runtime environments.

I hope the above information is helpful.