2

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)

1 Answer 1

1

Your block expression doesn't return a value in all cases.

Try this instead:

waitUntil { folder.exists() }
3
  • Same, I changed timeout to 10, and pointed folder to a known existing dir, it keeps waiting for 10 mins and timesout.
    – rodee
    May 9, 2020 at 13:51
  • I worked it around with waitUntil { def r = sh script: "[[ -d 'A/B' ]]", returnStatus: true return r == 0}
    – rodee
    May 9, 2020 at 13:53
  • Beware of using java or groovy File exists method and prefer fileExists 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
    Dec 19, 2023 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.