Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Pipeline waitUntil bash command returns certain string

I have a pipeline stage where I wait to get a certain string back from a sh script, and only when the strings match, continue to next stage, however, it doesn't work as expected:

node('master') {
    stage("wait for bash completion") {
        waitUntil {
            def output = sh returnStdout: true, script: 'cat /tmp/test.txt'
            output == "hello"
        }
    }
    stage("execute after bash completed") {
        echo "the file says hello!!!"
    }
}

The execution is something like that:

+ cat /tmp/test.txt
[Pipeline] }
Will try again after 0.25 sec
[Pipeline] {
[Pipeline] sh
[workspace] Running shell script
+ cat /tmp/test.txt
[Pipeline] }
Will try again after 0.3 sec
[Pipeline] {
[Pipeline] sh
[workspace] Running shell script
+ cat /tmp/test.txt
[Pipeline] }
Will try again after 0.36 sec
...
(so on and so forth)

What am I missing?

like image 565
Moshe Avatar asked Dec 31 '18 12:12

Moshe


2 Answers

From waitUntil's help:

Runs its body repeatedly until it returns true. If it returns false, waits a while and tries again. --

Your execution output looks exactly like that it is waiting for output == "hello" to match. Maybe the content of file /tmp/test.txt is not exactly hello. You might have some whitespace in it, e.g. new line as the last character.

like image 80
Travenin Avatar answered Sep 22 '22 17:09

Travenin


You probably need to add .trim() shell stdout to work, ie

def output = sh(returnStdout: true, script: 'cat /tmp/test.txt').trim()

Otherwise you end up with output showing a newline character at the end.

but probably, the better solution would be to use groovy script:

steps {
    sh "echo something > statusfile" // do something in the shell
    waitUntil(initialRecurrencePeriod: 15000) {
        script {
            def status = readFile(file: "statusfile")
            if ( status =~ "hello") {
                return true
            }else {
                println("no hello yet!")
                return false
            }
        }
    }
}
like image 29
shaftdiesel Avatar answered Sep 21 '22 17:09

shaftdiesel