Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.)

like image 280
Wilfred Hughes Avatar asked Oct 30 '22 05:10

Wilfred Hughes


1 Answers

In addition to the answer from @arifCee, you could do this:

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

node('general') {
    touchFiles()

    sh "ls"
}
like image 131
Jacob Avatar answered Nov 15 '22 04:11

Jacob