Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Retry logic for a particular step in Google cloud build

I have a google cloud build yaml file in which there is a particular step which gets failed sometimes. When the two cloud build runs at the same time. In this step it ssh to my VM via IAP tunnel to run a command. So is there any retry logic which I can mention on the Cloud build step so it will rerun that particular step only when it fails.

0 1 266
1 REPLY 1

Hi, @Robinwilliam15.

Google Cloud Build doesn’t natively support retrying individual steps using a retry key in the cloudbuild.yaml. Retries are generally handled at the build level by re-triggering the entire build, rather than for specific steps.

To implement step-level retries, you can create custom build steps or add retry logic within a script that you run in the step. This way, you can control the retry behavior for specific tasks. As of example -

steps:
- name: 'gcr.io/cloud-builders/gcloud'
  entrypoint: 'bash'
  args:
    - '-c'
    - |
      for i in {1..3}; do
        echo "Attempt $i"
        if ! ./deploy.sh; then
          echo "Deploy failed, retrying..."
          sleep 10
        else
          break
        fi
      done



Regards,
Mokit