<p>Your block expression doesn't return a value in all cases.</p>

<p>Try this instead:</p>

<pre><code>waitUntil { folder.exists() }
</code></pre>

CC BY-SA 4.0
2020-05-09T13:51:14.620Z
Same, I changed timeout to 10, and pointed `folder` to a known existing dir, it keeps waiting for 10 mins and timesout.
rodee
CC BY-SA 4.0
2020-05-09T13:53:05.847Z
I worked it around with `waitUntil {  def r = sh script: "[[ -d 'A/B' ]]", returnStatus: true
                                        return r == 0}`
rodee
CC BY-SA 4.0
2023-12-19T15:52:28.073Z
Beware of using java or groovy File exists method and prefer [fileExists](https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#fileexists-verify-if-file-exists-in-workspace) or shell.  Pipeline steps figure out that they should work on the agent.  The File methods may execute on controller where file is not.  CPS can be a challenge
Peter Kahn
CC BY-SA 4.0
2020-05-08T16:06:56.313Z
2020-05-08T16:06:56.313Z
jayhendren
<p>With this Jenkinsfile code, I am trying to test A/B folder creation, and also testing if waitUntil works till the folder is created, I expect it to wait for 0 secs as the folder is created before <code>waitUntil</code> gets called.</p>

<pre><code>stage('other job') {
                    steps {
                        script {
                            timeout(10) {
                                def folder = new File( 'A/B' )
                                println "Waiting for " + folder
                                println "fe==" + folder.exists()
                                waitUntil {
                                    if(folder.exists()) {
                                        return(true)
                                    }
                                }
                            }
                        }
                    }
                }
</code></pre>

<p>It fails with below error, what's wrong here? the folder is not gerrit created in workspace.</p>

<pre><code>Timeout set to expire in 10 min
[Pipeline] {
[Pipeline] // stage
[Pipeline] }
[Pipeline] echo
Waiting for A/B
[Pipeline] echo
fe==false
[Pipeline] waitUntil
[Pipeline] {
[Pipeline] }
[Pipeline] // waitUntil
[Pipeline] }
[Pipeline] // timeout
...
Failed in branch other job
...
[Pipeline] End of Pipeline
java.lang.ClassCastException: body return value null is not boolean
    at org.jenkinsci.plugins.workflow.steps.WaitForConditionStep$Callback.onSuccess(WaitForConditionStep.java:167)
    at org.jenkinsci.plugins.workflow.cps.CpsBodyExecution$SuccessAdapter.receive(CpsBodyExecution.java:377)
</code></pre>

<jenkins-pipeline><jenkinsfile><groovy>
Jenkinsfile script to waitUntil a folder is created
rodee
# Jenkinsfile script to waitUntil a folder is created

(Asked by: username_0 on 2020-05-07)

With this Jenkinsfile code, I am trying to test A/B folder creation, and also testing if waitUntil works till the folder is created, I expect it to wait for 0 secs as the folder is created before waitUntil gets called.
stage('other job') {
                    steps {
                        script {
                            timeout(10) {
                                def folder = new File( 'A/B' )
                                println "Waiting for " + folder
                                println "fe==" + folder.exists()
                                waitUntil {
                                    if(folder.exists()) {
                                        return(true)
                                    }
                                }
                            }
                        }
                    }
                }

It fails with below error, what's wrong here? the folder is not gerrit created in workspace.
Timeout set to expire in 10 min
[Pipeline] {
[Pipeline] // stage
[Pipeline] }
[Pipeline] echo
Waiting for A/B
[Pipeline] echo
fe==false
[Pipeline] waitUntil
[Pipeline] {
[Pipeline] }
[Pipeline] // waitUntil
[Pipeline] }
[Pipeline] // timeout
...
Failed in branch other job
...
[Pipeline] End of Pipeline
java.lang.ClassCastException: body return value null is not boolean
    at org.jenkinsci.plugins.workflow.steps.WaitForConditionStep$Callback.onSuccess(WaitForConditionStep.java:167)
    at org.jenkinsci.plugins.workflow.cps.CpsBodyExecution$SuccessAdapter.receive(CpsBodyExecution.java:377)

**Tags:** jenkins-pipeline, jenkinsfile, groovy


---

## 1 answers

### Answer by username_1 on 2020-05-08. Score: 1

Your block expression doesn't return a value in all cases.
Try this instead:
waitUntil { folder.exists() }

> **Comments:**
> **username_0**: Same, I changed timeout to 10, and pointed `folder` to a known existing dir, it keeps waiting for 10 mins and timesout.  
> **username_0**: I worked it around with `waitUntil {  def r = sh script: "[[ -d 'A/B' ]]", returnStatus: true                                         return r == 0}`  
> **username_2**: Beware of using java or groovy File exists method and prefer [fileExists](https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#fileexists-verify-if-file-exists-in-workspace) or shell.  Pipeline steps figure out that they should work on the agent.  The File methods may execute on controller where file is not.  CPS can be a challenge  

---

