Marcelo Idemax
Marcelo Idemax

Reputation: 2810

Jenkins Pipeline cannot execute SH command file in a Windows slave

I'm executing this code:

node('my_windows_slave') {
   sh 'ls'
}

In my Windows slave I can properly execute sh command:

enter image description here

But the pipeline script can't run the .sh file:

[Pipeline] sh
[D:\workspace\sandbox_pipeline] Running shell script
sh: D:\workspace\sandbox_pipeline@tmp\durable-2d7dd2f8\script.sh: command not found

What I could notice is that this .sh file is not even created, once I tried with bat and worked fined.

Any clue what could be the problem?

[UPDATE]

Jenkins somehow can't create the SH temporary file. Already checked the log, permissions, everything that came to my mind.

Upvotes: 9

Views: 18408

Answers (2)

forzagreen
forzagreen

Reputation: 2683

Use the bat step instead of sh.

From Jenkins docs:

Windows-based systems should use the bat step for executing batch commands.

Upvotes: 6

Marcelo Idemax
Marcelo Idemax

Reputation: 2810

I will leave my workaround as an answer for while before approve it once I'm still not 100% sure about the root cause and might someone else show up with a elegant solution...

def shell(command) {
    return bat(returnStdout: true, script: "sh -x -c \"${command}\"").trim()
}

Attention

You still executing SH commands in a CMD, it means some %d for example can break your SH command.

Upvotes: 9

Related Questions