How do I run multiple sh commands in a function in a Jenkinsfile?

Viewed 6177

Given the following Jenkinsfile:

@NonCPS
void touchFiles() {
    sh "touch foo"
    sh "touch bar"
}

node('general') {
    touchFiles()

    sh "ls"
}

I see the following in the console log on Jenkins:

Running on box35 in /internal/slave/build/workspace/1499786637.EXAMPLE_test-repo_test_jenkinsfile
[Pipeline] {
[Pipeline] sh
[1499786637.EXAMPLE_test-repo_test_jenkinsfile] Running shell script
+ touch foo
[Pipeline] sh
[1499786637.EXAMPLE_test-repo_test_jenkinsfile] Running shell script
+ ls
foo

Note that 'bar' has not been created.

Why does only the first sh run here? How can I factor out a series of sh calls in a Jenkinsfile?

(I know I could write sh "touch foo; touch bar" but in more complex Jenkinsfiles I sometimes need separate sh invocations.)

2 Answers
Related