Search code examples
google-cloud-platformgoogle-cloud-buildgoogle-container-registrycloudbuild.yaml

How to store docker images in Container Registry with different tags with a config file using Cloudbuild?


I want to store in the Google Container Registry the image with two different tags $BRANCH_NAME-$REVISION_ID and latest

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/myapp:$BRANCH_NAME-$REVISION_ID', '.']
- name: 'gcr.io/cloud-builders/docker'
  args: ['push', 'gcr.io/$PROJECT_ID/myapp:$BRANCH_NAME-$REVISION_ID']
images: ['gcr.io/$PROJECT_ID/myapp:$BRANCH_NAME-$REVISION_ID']

I am able to save it with a single tag, but it fails when I try to add a second tag. I get the following error

Finished Step #1
Starting Step #2
Step #2: Already have image (with digest): gcr.io/cloud-builders/docker
Step #2: The push refers to repository [gcr.io/myproject/myapp]
Step #2: tag does not exist: gcr.io/myproject/myapp:latest

I want to do this to be sure that my k8s deployment file is pointing to the latest image.


UPDATE

I was able to do it

substitutions:
  _IMG_NAME: "myapp"

steps:

- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/${_IMG_NAME}:$BRANCH_NAME-$REVISION_ID', '-t' , 'gcr.io/$PROJECT_ID/${_IMG_NAME}:latest', '.']
images: ['gcr.io/$PROJECT_ID/${_IMG_NAME}']

Solution

  • I think it may be possible to use/set a few tags when the docker image is created. For example, something similar can be used:

    - name: 'gcr.io/cloud-builders/docker'
      env:
        - 'DOCKER_BUILDKIT=1'
      args:
        - build
        - --tag
        - gcr.io/$PROJECT_ID/myapp:$BRANCH_NAME-$REVISION_ID'
        - --tag
        - gcr.io/$PROJECT_ID/myapp:latest
        - .
    

    I also would guess that one of either docker push or image should be enough in your case.