Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 523
Idemax Avatar asked Jan 11 '17 16:01

Idemax


People also ask

What is SH in Jenkins pipeline?

On Linux, BSD, and Mac OS (Unix-like) systems, the sh step is used to execute a shell command in a Pipeline. Jenkinsfile (Declarative Pipeline) pipeline { agent any stages { stage('Build') { steps { sh 'echo "Hello World"' sh ''' echo "Multiline shell steps works too" ls -lah ''' } } } }

Can Jenkins run shell script?

you can execute shell in jenkins as a build step like this. echo "Hello World!" With previous action our server will not ask for password when we execute that command. If you need to execute a shell script in another server, then ssh is your best friend.


2 Answers

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.

like image 132
Idemax Avatar answered Oct 11 '22 00:10

Idemax


Use the bat step instead of sh.

From Jenkins docs:

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

like image 21
forzagreen Avatar answered Oct 11 '22 01:10

forzagreen