8

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.

StephenKing
  • 36,187
  • 11
  • 83
  • 112
Idemax
  • 2,712
  • 6
  • 33
  • 66
  • Set Env Varibale and do sysmlink to nohup.exe as suggested below https://stackoverflow.com/a/45151156/3648023 After this setup you can use sh directly on your jenkinsfile – startprogramming May 07 '19 at 16:09

2 Answers2

8

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.

Idemax
  • 2,712
  • 6
  • 33
  • 66
7

Use the bat step instead of sh.

From Jenkins docs:

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

forzagreen
  • 2,509
  • 30
  • 38
  • why once I want to use the shell DLL instead of the BAT from win?! – Idemax Jul 24 '17 at 14:11
  • It's about good practices. `sh` is not a native Windows command, and the official docs recommends using `bat`. – forzagreen Jul 24 '17 at 16:10
  • its nonsense, sry, if you have something that is easy done my SHELL why not use it? why do not use something that is cross platform? There is nothing to do with best practices but what you like or not... – Idemax Jul 25 '17 at 13:34
  • 1
    Spent hours trying to get shell to work, until I found this. Thanks, take my money. ;) – Rutwick Gangurde Apr 15 '21 at 18:49