skip to Main Content

I want to deploy an image from cloud run to kubernetes cluster. I have successfully built the docker image in cloudbuild.yaml file. Now the challenge is how to use the image in deploy.yaml

The task fails with errors indicating the the variables were not substituted as expected.

Here are the config excerpts:

cloudbuild.yaml

//push image to container registry
    ....
    .......

  - id: "deploy container image to GKE"
    name: "gcr.io/cloud-builders/gke-deploy"
    args: [
    "--cluster", "${_CLUSTER_NAME}",
    "run",
    "--filename", "deployment.yaml",
    "--image", "us-west1-docker.pkg.dev/$PROJECT_ID/abc/abc-server-${_DEPLOYMENT_TYPE}:$COMMIT_SHA",
    "--location", "${_COMPUTE_ZONE}"
    ]

deployment.yaml

    ....
    ........
    spec:
      containers:
        - name: idr-server-k8ts-test
          image: us-west1-docker.pkg.dev/$PROJECT_ID/abc/abc-server-${_DEPLOYMENT_TYPE}:$COMMIT_SHA
          ports:
            - containerPort: 8080

Vars defined in build trigger

_CLUSTER_NAME
_DEPLOYMENT_TYPE
_COMPUTE_ZONE

I get the error:

Error: failed to prepare deployment: failed to update container of objects: failed to parse reference from image "us-west1-docker.pkg.dev/$PROJECT_ID/abc/abc-server-${_DEPLOYMENT_TYPE}:$COMMIT_SHA": could not parse reference: us-west1-docker.pkg.dev/$PROJECT_ID/abc/abc-server-${_DEPLOYMENT_TYPE}:$COMMIT_SHA

2

Answers


  1. Kubernetes doesn’t understand any variables written in manifest files. Your image’s name (us-west1-docker.pkg.dev/$PROJECT_ID/abc/abc-server-${_DEPLOYMENT_TYPE}:$COMMIT_SHA) contains environment variables. Plain manifests require constant/hardcoded values.

    If you want to populate your manifests dynamically through environment variables, create a helm chart and apply it in the cluster.

    Login or Signup to reply.
  2. Try once like with using the ""

    - name: "gcr.io/cloud-builders/gke-deploy"
      args:
      - run
      - --filename=deployment.yaml
      - --image=harshmanvar/gdg-rajkot:$SHORT_SHA
      - --expose=80
      - --app=nodejs-hello-demo
      - --location=asia-east1-a
      - --cluster=cluster-1
    

    Extra option :

    cloudbuild.yaml

    substitutions:
        _CLOUDSDK_COMPUTE_ZONE: us-central1-c  # default value
        _CLOUDSDK_CONTAINER_CLUSTER: standard-cluster-1      # default value
    steps:
    - id: 'set test core image in yamls'
      name: 'ubuntu'
      args: ['bash','-c','sed -i "s,TEST_IMAGE_NAME,gcr.io/$PROJECT_ID/$REPO_NAME/$BRANCH_NAME:$SHORT_SHA," deployment.yaml']
    - name: 'gcr.io/cloud-builders/kubectl'
      args: ['apply', '-f', 'deployment.yaml']
      env:
      - 'CLOUDSDK_COMPUTE_ZONE=${_CLOUDSDK_COMPUTE_ZONE}'
      - 'CLOUDSDK_CONTAINER_CLUSTER=${_CLOUDSDK_CONTAINER_CLUSTER}'
    

    deployment.yaml

    containers:
          - image: TEST_IMAGE_NAME
            name: test-image
            ports:
    

    So cloud build will replace the TEST_IMAGE_NAME in deployment.yaml and updated file will get deployed to GKE.

    You can find the example : https://github.com/harsh4870/basic-ci-cd-cloudbuild

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search