Cyberithub

Solved "failed unmarshalling build config. line xx: did not find expected key"

Advertisements

Recently while working in a deployment I was getting "failed unmarshalling build config. line xx: did not find expected key" error. While this error is quite common during Cl/CD deployment, I will cover this error from Cloud Run perspective. If you are not aware what is Cloud Run then let me give you a brief information about this. Cloud Run is a managed compute platform that enables us to run stateless containers that are invocable via web requests or pub/sub events. You can read more about this on Google Documentation.

Solved "failed unmarshalling build config. line xx: did not find expected key"

Solved "failed unmarshalling build config. line xx: did not find expected key"

This error might come due to incorrect structure or due to extra space or may be due to incorrect indentation in Cloud build Script. To find the root cause of this error, we always need to start with the Line number that we get in the error. For example: you might get this error from Line 24 of your Cloud build Script. In that case it will look like "failed unmarshalling build config. Line 24: did not find expected key" error. In my case, I was getting same error from below marked Line which is a Cloud Run deployment step.

cyberithub@cloudshell:~/sample-project-repo (sample-project-308612)$ vi cloudbuild.yaml
steps:
- name: 'gcr.io/cloud-builders/git'
args: ['clone','https://source.developers.google.com/p/${_PROJECT}/r/${_REPO_NAME}']

- name: 'docker.io/library/python:3.7'
id: test
entrypoint: /bin/sh
args: ['-c','pip install pytest && pip install -r requirements.txt && pytest test.py']

- name: 'gcr.io/cloud-builders/docker'
args: ['build','-t','gcr.io/${_PROJECT}/${_CONTAINERNAME}/${_VERSION}','.']

- name: 'gcr.io/cloud-builders/docker'
args: ['push','gcr.io/${_PROJECT}/${_CONTAINERNAME}/${_VERSION}']

- name: 'gcr.io/cloud-builders/gcloud'
args: ['config','set','project','${_PROJECT}']

- name: 'gcr.io/cloud-builders/gcloud'
args: ['run','deploy','demo-service','--image','gcr.io/${_PROJECT}/${_CONTAINERNAME}/${_VERSION}','--region','us-east1','--platform','managed']
images:
- 'gcr.io/${_PROJECT}/${_CONTAINERNAME}/${_VERSION}'

substitutions:
_PROJECT: sample-project-308612
_REPO_NAME: sample-project-repo
_CONTAINERNAME: sample-container
_VERSION: '1.1'

While there is nothing fundamentally wrong in the above line but it was still throwing same error whenever I tried to deploy above cloud build script. This Script is basically cloning the repo from Google Source Repositories, performing the Python Unit testing using Pytest module, then building the docker image and pushing it to Google Container Registry(GCR). Using this docker image, a service is getting created and hosted in Cloud Run.

NOTE:

It is important to note that the solution given here for "failed unmarshalling build config. Line 24: did not find expected key" error worked fine for me but it might be possible that same does not work for you. In that case you need to further debug this error and find the root cause.

After doing further analysis, I understood that the Cloud Run is expecting some more keys to be added like CPU, Memory and timeout values. These values are currently missing in the step. So I have added the required keys and now Cloud Run argument looks like below.

args: ['run','deploy','demo-service','--image','gcr.io/${_PROJECT}/${_CONTAINERNAME}/${_VERSION}','--region','us-east1','--platform','managed','--memory','8Gi','--cpu','8','--timeout','300']

After adding above parameters, now our complete cloudbuild.yaml file will look like below. If you try to deploy this file then you should not get "failed unmarshalling build config. Line 24: did not find expected key" error again. You can check more about Cloud Run on Google Cloud Documentation Page.

cyberithub@cloudshell:~/sample-project-repo (sample-project-308612)$ vi cloudbuild.yaml
steps:
- name: 'gcr.io/cloud-builders/git'
args: ['clone','https://source.developers.google.com/p/${_PROJECT}/r/${_REPO_NAME}']

- name: 'docker.io/library/python:3.7'
id: test
entrypoint: /bin/sh
args: ['-c','pip install pytest && pip install -r requirements.txt && pytest test.py']

- name: 'gcr.io/cloud-builders/docker'
args: ['build','-t','gcr.io/${_PROJECT}/${_CONTAINERNAME}/${_VERSION}','.']

- name: 'gcr.io/cloud-builders/docker'
args: ['push','gcr.io/${_PROJECT}/${_CONTAINERNAME}/${_VERSION}']

- name: 'gcr.io/cloud-builders/gcloud'
args: ['config','set','project','${_PROJECT}']

- name: 'gcr.io/cloud-builders/gcloud'
args: ['run','deploy','demo-service','--image','gcr.io/${_PROJECT}/${_CONTAINERNAME}/${_VERSION}','--region','us-east1','--platform','managed','--memory','8Gi','--cpu','8','--timeout','300']
images:
- 'gcr.io/${_PROJECT}/${_CONTAINERNAME}/${_VERSION}'

substitutions:
_PROJECT: sample-project-308612
_REPO_NAME: sample-project-repo
_CONTAINERNAME: sample-container
_VERSION: '1.1'

Leave a Comment