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
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.
Try once like with using the
""
Extra option :
cloudbuild.yaml
deployment.yaml
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