If you are using GCB, you likely encountered cases where you needed to continue the process regardless of the result of a particular step. Naturally, builds fail if a step terminates with a non-zero exit code.
Here is an example that pulls a container image. If the image does not exist, it’s okay to continue. Your steps may look different, more involved or use another command.
steps:
- name: 'gcr.io/cloud-builders/docker'
entrypoint: 'bash'
args: ['-c', 'docker pull eu.gcr.io/project/myimage:$TAG || exit 0']
If your step hides if it fails because it is OK to let it fail, I have a better option for you.
What is the problem with the code above?
There are two problems with ignoring the exit code of the step.
- The step will lie about the exit code, and you will not be able to see when the command fails. Seeing an always passing step provides a false assurance that everything is fine.
- The step is convoluted by the error handling code. It is no longer possible to glance at the
args
and know what the step will do.
Let it fail
The Google Cloud Build configuration schema has a ton of underutilised features.
There are two handy features for this situation. Please note that both of them are experimental.
These options allow you to tell the runner to ignore if the step fails. With the latter, you can even specify which error codes are allowed.
steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['pull', 'eu.gcr.io/project/myimage:$TAG']
allowFailure: true
- name: 'gcr.io/cloud-builders/docker'
args: ['pull', 'eu.gcr.io/project/myimage:$TAG']
allowExitCodes: [1]
Alternatives
If this solution is not for you, it may be worth looking at the script
property and how you can use it to run bash scripts .
steps:
- name: 'gcr.io/cloud-builders/docker'
script: |
docker pull eu.gcr.io/project/myimage:$TAG || exit 0