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

How to execute a step based on the result of the previous step

Hello, I'm trying to put together a cloudbuild.yaml to deploy a helm chart on a GKE. I advanced and I was able to do the deployment but now I want to be able to control the result, in case the Helm deployment does not end well, I want to do an uninstall and have it finish and if it has been successful, continue with another point.
What I don't understand how to do is that step choice based on the result of a previous step. Any idea how I could do it?

This is my cloudbuild.yaml.

options:
env:
- 'CLOUDSDK_CORE_PROJECT=xxxxxxxxxxxx'
- 'CLOUDSDK_COMPUTE_REGION=xxxxxxxxxxxxxxxxxxx'
- 'CLOUDSDK_CONTAINER_CLUSTER=xxxxxxxxxxxxxxxxxxxxx'
steps:
- name: 'gcr.io/$PROJECT_ID/helm:latest'
args: ['install', 'helloworld', './hello-world', '-n', 'pruebas']
id: 'STEP 1 - Helm installation'

- name: 'gcr.io/cloud-builders/kubectl'
entrypoint: 'bash'
args:
- '-c'
- |
if kubectl rollout status deployment helloworld-hello-world -n pruebas; then
echo "Rollout completed successfully."
else
echo "Rollout failed. Executing failure step."
fi
id: 'STEP 2 - Helm installation validation'

STEP 2 and STEP 4 should be executed only if STEP2 fail

- name: 'gcr.io/$PROJECT_ID/helm:latest'
args: ['uninstall', 'helloworld', '-n', 'pruebas']
id: 'STEP 3 - Helm UNINSTALL'

- name: 'google/cloud-sdk:slim'
args: ['echo', 'END NOK']
id: 'STEP 4 - INSTALLATION FAILED'

STEP 5 should be executed only if STEP2 is successful
- name: 'google/cloud-sdk:slim'
args: ['echo', 'END OK']
id: 'STEP % - INSTALLATION OK'
waitFor:
- 'Helm installation validation'

Thanks for your help

Regards,

Pablo

 

0 2 1,958
2 REPLIES 2

Hi @pabloabdala,

Welcome to Google Cloud Community!

Conditional Execution in Cloud Build:

You want to execute steps based on the success or failure of previous steps in your cloudbuild.yaml. Here's how to achieve this:

1. Use waitFor: Subsequent steps can specify which previous steps they must wait for before running.

2. Control Exit Codes: Ensure each step returns an exit code (0 for success, non-zero for failure). Cloud Build uses these codes to decide which subsequent steps to run.

3. Utilize Conditional Logic: Employ scripts like Bash within steps to implement more complex decision-making based on exit codes or other factors.

Revised Approach:

  • Your STEP 3 (uninstall) only runs if STEP 2 (validation) fails (waits for it).
  • Your STEP 5 (success message) only runs if STEP 2 (validation) succeeds (waits for it).

By combining waitFor and exit codes, you can achieve conditional execution based on previous steps' results.

Thanks @christianpaula it was very usefull.  I used your logic and I used the workspace to pass variables amoung steps. I copy here my cloudbuild.yaml to help someone who need it.

 

 

options:
env:
- 'CLOUDSDK_CORE_PROJECT=cluster1-406519'
- 'CLOUDSDK_COMPUTE_REGION=southamerica-east1'
- 'CLOUDSDK_CONTAINER_CLUSTER=autopilot-cluster-1'
steps:
- name: 'gcr.io/$PROJECT_ID/helm:latest'
args: ['install', 'helloworld', './hello-world', '-n', 'pruebas']
id: 'Helm installation'

- name: 'gcr.io/cloud-builders/kubectl'
entrypoint: 'bash'
args:
- '-c'
- |
if kubectl rollout status deployment helloworld-hello-world -n pruebas --timeout 180s; then
echo "Rollout completed successfully."
else
echo "Rollout failed. Executing failure step."
echo "failed" > /workspace/helm_installation.txt
fi
id: 'stepA'

- name: 'gcr.io/$PROJECT_ID/helm:latest'
entrypoint: 'bash'
args:
- '-c'
- |
if grep -q "fail" /workspace/helm_installation.txt; then
helm uninstall helloworld -n pruebas
echo "Helm uninstalled"
exit 1
fi