user2196409 user2196409 - 2 months ago 29
Bash Question

Jenkins shell not working from within Seed DSL generated Job

new to Groovy, Closures & Jenkins.

I have created a seed job:

def Job1 = 'FromTemplate-testJob'
job {
name Job1
steps {
shell( "echo Hello > out5.txt" )
shell( "/c echo custard > op4.txt")
}
}


which as expected successfully creates a sub-job containing the 2 shell commands:

echo Hello > out5.txt
/c echo custard > op4.txt


However, when run, this created job apparently runs successfully with the following output:

Started by user anonymous
Building in workspace C:\Program Files (x86)\Jenkins\jobs\FromTemplate-testJob\workspace
[workspace] $ C:\Windows\system32\cmd.exe -xe C:\Windows\TEMP\hudson3852539874278383422.sh
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Program Files (x86)\Jenkins\jobs\FromTemplate-testJob\workspace>[workspace] $ C:\Windows\system32\cmd.exe -xe C:\Windows\TEMP\hudson1697067517687695305.sh
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Program Files (x86)\Jenkins\jobs\FromTemplate-testJob\workspace>Finished: SUCCESS


However neither of the output files are not created anywhere. The shell executable is defined as:

C:\Windows\system32\cmd.exe


What don't I understand?

Answer

shell dsl command is translated to "Execute shell" build step, which usually is used on Unix-like systems. You have to use batchFile dsl command instead, so it will be translated to "Execute Windows batch command" build step, which is used for Windows:

def Job1 = 'FromTemplate-testJob'
job {
    name Job1
    steps {
        batchFile( "echo Hello > out5.txt" )
        batchFile( "echo custard > op4.txt" )
    }
}