Jenkinsfile script to waitUntil a folder is created

5,539

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

Try this instead:

waitUntil { folder.exists() }
Share:
5,539

Related videos on Youtube

rodee
Author by

rodee

Updated on September 18, 2022

Comments

  • rodee
    rodee over 1 year

    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)
    
  • rodee
    rodee almost 4 years
    Same, I changed timeout to 10, and pointed folder to a known existing dir, it keeps waiting for 10 mins and timesout.
  • rodee
    rodee almost 4 years
    I worked it around with waitUntil { def r = sh script: "[[ -d 'A/B' ]]", returnStatus: true return r == 0}