How to Use Jenkins waitUntil Step to Wait for a Condition Before Proceeding
Description: Learn how to configure Jenkins Pipeline's waitUntil step to wait for a specific condition, including proper handling of shell command outputs to avoid infinite loops.
---
This video is based on the question https://stackoverflow.com/q/78924209/ asked by the user 'Patlatus' ( https://stackoverflow.com/u/1517187/ ) and on the answer https://stackoverflow.com/a/78925520/ provided by the user 'NO PLAY' ( https://stackoverflow.com/u/12413209/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: How to configure Jenkins step to wait until certain condition holds true before continuing with other steps?
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.
If anything seems off to you, please feel free to drop me a comment under this video.
---
Introduction
When orchestrating CI/CD pipelines in Jenkins, you might need to pause execution until a specific condition is met. For example, in Salesforce environments, you may want to delay running Apex unit tests until permission set groups finish recalculating.
This poses a challenge: how can Jenkins reliably wait for a condition like "no PermissionSetGroup is updating" before proceeding with further steps?
Problem Overview
A typical Jenkins stage uses the waitUntil step combined with a shell command to check the condition:
[[See Video to Reveal this Text or Code Snippet]]
However, this can cause infinite looping even when the count is zero, because the condition never returns true in Jenkins due to subtle issues with string comparison and output format.
Key Issues to Fix
Output Contains Trailing Whitespace or Newlines:
The sh step often returns strings with trailing \n or spaces, making direct comparison (r == 0) fail.
Data Type Mismatch:
sh returns a string; comparing it directly to integer 0 causes the condition to never be true.
Concise Solution
1. Trim the Output
Use Groovy's .trim() method to remove extraneous whitespace:
[[See Video to Reveal this Text or Code Snippet]]
2. Compare Strings Properly
Check the trimmed output against string '0':
[[See Video to Reveal this Text or Code Snippet]]
Example Revised Stage
[[See Video to Reveal this Text or Code Snippet]]
Notes
Use triple quotes ''' for multiline shell commands for better readability.
Use .trim() every time you are capturing shell command output to avoid hidden whitespace issues.
Always compare string output with string literals in Groovy scripts.
Summary
To have Jenkins waitUntil reliably pause until a condition is true:
Trim your shell command output to avoid whitespace issues.
Compare strings against string values, not integers.
This straightforward fix prevents infinite loops caused by subtle whitespace or type mismatches when waiting on external system states in Jenkins pipelines. Close description
---
This video is based on the question https://stackoverflow.com/q/78924209/ asked by the user 'Patlatus' ( https://stackoverflow.com/u/1517187/ ) and on the answer https://stackoverflow.com/a/78925520/ provided by the user 'NO PLAY' ( https://stackoverflow.com/u/12413209/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: How to configure Jenkins step to wait until certain condition holds true before continuing with other steps?
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.
If anything seems off to you, please feel free to drop me a comment under this video.
---
Introduction
When orchestrating CI/CD pipelines in Jenkins, you might need to pause execution until a specific condition is met. For example, in Salesforce environments, you may want to delay running Apex unit tests until permission set groups finish recalculating.
This poses a challenge: how can Jenkins reliably wait for a condition like "no PermissionSetGroup is updating" before proceeding with further steps?
Problem Overview
A typical Jenkins stage uses the waitUntil step combined with a shell command to check the condition:
[[See Video to Reveal this Text or Code Snippet]]
However, this can cause infinite looping even when the count is zero, because the condition never returns true in Jenkins due to subtle issues with string comparison and output format.
Key Issues to Fix
Output Contains Trailing Whitespace or Newlines:
The sh step often returns strings with trailing \n or spaces, making direct comparison (r == 0) fail.
Data Type Mismatch:
sh returns a string; comparing it directly to integer 0 causes the condition to never be true.
Concise Solution
1. Trim the Output
Use Groovy's .trim() method to remove extraneous whitespace:
[[See Video to Reveal this Text or Code Snippet]]
2. Compare Strings Properly
Check the trimmed output against string '0':
[[See Video to Reveal this Text or Code Snippet]]
Example Revised Stage
[[See Video to Reveal this Text or Code Snippet]]
Notes
Use triple quotes ''' for multiline shell commands for better readability.
Use .trim() every time you are capturing shell command output to avoid hidden whitespace issues.
Always compare string output with string literals in Groovy scripts.
Summary
To have Jenkins waitUntil reliably pause until a condition is true:
Trim your shell command output to avoid whitespace issues.
Compare strings against string values, not integers.
This straightforward fix prevents infinite loops caused by subtle whitespace or type mismatches when waiting on external system states in Jenkins pipelines. Close description
