1

Is it possible in some manner to gracefully exit/break in some middle step of GCB?

If some step command exits with non success code, build flow will break, but will also be considered failed. I'd like to break but maintain a success status.

Mugen
  • 8,301
  • 10
  • 62
  • 140

2 Answers2

7

I found a solution which I consider a better workaround: let the build cancel itself using gcloud sdk.

  - name: 'gcr.io/cloud-builders/gcloud'
    id: 'Cancel current build if on master'
    entrypoint: 'sh'
    args:
      - '-c'
      - |
        test $BRANCH_NAME = "master" && gcloud builds cancel $BUILD_ID > /dev/null || true

Note the service account that runs the builds (xxx@cloudbuild.gserviceaccount.com) should have appropriate permissions to cancel build.

.

Mugen
  • 8,301
  • 10
  • 62
  • 140
  • After implementing it myself I found out there's also an existing community cloud-builder exactly for this task: https://github.com/GoogleCloudPlatform/cloud-builders-community/tree/master/cancelot – Mugen Nov 02 '20 at 12:13
  • 1
    I was getting the occasional issue where despite cancelling it would move to the next step quicker than the API could cancel (???). I ended up use `sleep 1000` to block until the API completed the request. This seemed to work but feels dirty. – Ari Sep 07 '21 at 02:36
  • 1
    @Ari Use `waitFor`? https://cloud.google.com/build/docs/configuring-builds/configure-build-step-order – Jack Apr 10 '22 at 13:59
  • `gcloud builds cancel $BUILD_ID && exit 1;` did the trick for me. (sleep was just ignored, dunno why). So it stopped at that moment, and did not move to the next step.. And the overall status of the build was cancelled as desired, not failed! – Bence Kővári Mar 31 '23 at 10:51
2

That's something which is not possible at the moment (even though it's an option that will be added in the future), but you can use a workaround to ignore the failure of any step. Using bash you achieve that by using something like the following:

- name: 'gcr.io/cloud-builders/docker'
  entrypoint: 'bash'
  args:
  - '-c'
  - |
    docker pull gcr.io/$PROJECT_ID/my-image || exit 0

Here you can find the link to the Github issue where the same question was addressed and you will be able to find more information.

bhito
  • 2,083
  • 7
  • 13
  • .. And here is it on [issuetracker](https://issuetracker.google.com/issues/128353446), still not implemented years away – vladli Sep 26 '22 at 14:48
  • 1
    seems they finally released it as a preview! https://cloud.google.com/build/docs/release-notes#November_18_2022 – Mugen Feb 09 '23 at 08:26