0
votes

I'm working on my first cloudbuild.yaml file and running into this error:

Your build failed to run: failed unmarshalling build config cloudbuild.yaml: yaml: line 8: did not find expected key

Here are the contents of my file (comments omitted), I have a few questions afterwards:

steps:
- name: 'node:12-alpine'
    entrypoint: 'bash'
    args:
        - 'build.sh'
- name: 'docker'
    args:
    - 'build'
    - '-t'
    - 'gcr.io/$PROJECT_ID/my-project:$(git describe --tags `git rev-list --tags --max-count=1`)'
images: ['gcr.io/$PROJECT_ID/my-project']

Questions:

  • The line with - name: 'node:12-alpine' seems to be where it's blowing up. However, the documentation states, "Cloud Build enables you to use any publicly available image to execute your tasks.". The node:12-alpine imgage is publicly available so what am I doing wrong?
  • Secondly, I'm trying to execute a file with a bunch of BASH commands in the first step. That should work, provided the commands are all supported by the Alpine image I'm using, right?
  • Lastly, I'm trying to create a docker image with a version number based on the version of the latest git tag. Is syntax like this supported, or how is versioning normally handled with google cloud build (I saw nothing on this topic looking around)
2
How do you trigger your build? Manually (gcloud command) or automatically (use Cloud Build Trigger) - guillaume blaquiere
Think you've missed the question @guillaumeblaquiere... The build is triggering, it's just not working, because of some mercurial problem with the syntax. Hence my question.... - quickshiftin
No, the 3rd point depends on the way that you start your build! So? - guillaume blaquiere
The build is being triggered automatically, when I push to my github repository. - quickshiftin
Incidentally, debugging with manual submissions is much faster - quickshiftin

2 Answers

1
votes

When you run a container into Cloud Build, the entry point defined into the container is automatically called, and the args are passed in parameter of this entry point.

What you have to know

  • You can override the entrypoint, as you did in the node:12 image
  • If the container doesn't contain an entrypoint, the build failed (Your error, you use a generic docker image). You can
    • Either define the correct entrypoint (here entrypoint: "docker")
    • Or use a Cloud Builder, for docker, this one - name: 'gcr.io/cloud-builders/docker'
  • The steps' args are forwarded as-is, without any interpretation (except variable replacement like $MyVariable). Your command interpretation $(my command) isn't evaluated. Except is you do this
- name: 'gcr.io/cloud-builders/docker' #you can also use the raw docker image here
  entrypoint: 'bash'
  args:
    - '-c'
    - |
      first bash command line
      second bash command line
      docker build -t gcr.io/$PROJECT_ID/my-project:$(git describe --tags `git rev-list --tags --max-count=1`)

But you can get the tag smarter. If you look at the default environment variable of Cloud Build, you can use $TAG_NAME.

docker build -t gcr.io/$PROJECT_ID/my-project:$TAG_NAME

Be careful, it's true only if you trigger it from your repository. If you run a manual build it doesn't work. So, there is a workaround. Look at this

- name: 'gcr.io/cloud-builders/docker' 
  entrypoint: 'bash'
  args:
    - '-c'
    - |
      TAG=${TAG_NAME}
      if [ -z $${TAG} ]; then TAG=$(git describe --tags `git rev-list --tags --max-count=1`); fi      
      docker build -t gcr.io/$PROJECT_ID/my-project:$${TAG}

However, I don't recommend you to override your images. You will lost the history, and if you override a good image with a wrong version, you loose it!

If you didn't catch some part, like why the double $$ and so on, don't hesitate to comment

1
votes

This error is most probably caused because of bad indentation of your cloudbuild.yaml file. You can take a look at official documentation which shows the structure of this file:

steps:
- name: string
  args: [string, string, ...]
  entrypoint: string
- name: string
  ...
- name: string
  ...
images:
- [string, string, ...]