jenkinsjenkins-pipeline

How to configure Jenkins step to wait until certain condition holds true before continuing with other steps?


How to configure Jenkins step to wait until certain condition holds true before continuing with other steps?

In this particular case we want to postpone execution of unit tests until Permission Set Groups are recalculated

         stage('Run Apex Tests') {
            steps {
                timeout(5) {
                    waitUntil {
                        script {
                            def r = sh script: 'sf data query -q "SELECT COUNT(Id) FROM PermissionSetGroup WHERE Status = \'Updating\'" --json | jq \'.result.records[0].expr0\' -r', returnStdout: true
                            echo "SELECT COUNT(Id) FROM PermissionSetGroup WHERE Status = \'Updating\': $r"
                            def x = (r == 0)
                            echo "r == 0? $x"
                            return (r == 0);
                        }
                    }
                }
                script {
                    dir("${BASE_DIR}") {
                        sh "ls -la"
                        sh "mkdir test-results/"
                        sh "sf apex run test --target-org \"${env.GIT_BRANCH}\" --code-coverage --detailed-coverage --result-format junit --output-dir test-results/ --wait 500"
                    }
                }
            }
        }

However, looks like the permission set groups are already recalculated and the return should return true but it is not working and looping instead

+ + sf data query -qjq SELECT COUNT(Id) FROM PermissionSetGroup WHERE Status = 'Updating' .result.records[0].expr0 --json -r

 [33m›[39m   Warning: @salesforce/cli update available from [92m2.47.6[39m to [92m2.55.6[39m.
[Pipeline] echo
SELECT COUNT(Id) FROM PermissionSetGroup WHERE Status = 'Updating': 0

[Pipeline] }
[Pipeline] // script
[Pipeline] }
Will try again after 15 sec
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ sf data query -q SELECT COUNT(Id) FROM PermissionSetGroup WHERE Status = 'Updating' --json
+ jq .result.records[0].expr0 -r
 [33m›[39m   Warning: @salesforce/cli update available from [92m2.47.6[39m to [92m2.55.6[39m.
[Pipeline] echo
SELECT COUNT(Id) FROM PermissionSetGroup WHERE Status = 'Updating': 0

[Pipeline] }
[Pipeline] // script
[Pipeline] }
Will try again after 15 sec
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ sf data query -q SELECT COUNT(Id) FROM PermissionSetGroup WHERE Status = 'Updating' --json
+ jq .result.records[0].expr0 -r
 [33m›[39m   Warning: @salesforce/cli update available from [92m2.47.6[39m to [92m2.55.6[39m.
[Pipeline] echo
SELECT COUNT(Id) FROM PermissionSetGroup WHERE Status = 'Updating': 0

[Pipeline] }
[Pipeline] // script
[Pipeline] }
Will try again after 15 sec
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ sf data query -q SELECT COUNT(Id) FROM PermissionSetGroup WHERE Status = 'Updating' --json
+ jq .result.records[0].expr0 -r
 [33m›[39m   Warning: @salesforce/cli update available from [92m2.47.6[39m to [92m2.55.6[39m.
[Pipeline] echo
SELECT COUNT(Id) FROM PermissionSetGroup WHERE Status = 'Updating': 0

[Pipeline] }
[Pipeline] // script
[Pipeline] }
Will try again after 15 sec
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ sf data query -q SELECT COUNT(Id) FROM PermissionSetGroup WHERE Status = 'Updating' --json
+ jq .result.records[0].expr0 -r
 [33m›[39m   Warning: @salesforce/cli update available from [92m2.47.6[39m to [92m2.55.6[39m.
[Pipeline] echo
SELECT COUNT(Id) FROM PermissionSetGroup WHERE Status = 'Updating': 0

What is wrong here?


Solution

  • IMO, there are two important things to correct in your pipeline.

    1. Trimmed Output: Add .trim() to the sh command output to remove any extra whitespace.
    2. String Comparison: Change the comparison to r == '0' to ensure it compares strings correctly.