sh command is not cross platform on windows nodes

214 views
Skip to first unread message

red 888

unread,
Jun 29, 2018, 12:17:05 PM6/29/18
to Jenkins Users
On my windows master if I run this command
sh script: "whoami"

It uses cmd. 

But on a linux slave it will use bash

But on a windows slave it fails with: 
java.io.IOException: CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.<init>(Unknown Source) at java.lang.ProcessImpl.start(Unknown Source)

How do I get the slave node to use "cmd" for the sh command?




Mark Waite

unread,
Jun 29, 2018, 12:39:35 PM6/29/18
to jenkins...@googlegroups.com
Insert a conditional check for isUnix and use "bat" instead of "sh".  See https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#isunix-checks-if-running-on-a-unix-like-node for more details.

Mark Waite

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/7113f534-8de0-4b8e-87b7-59e876216c3d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sverre Moe

unread,
Jun 30, 2018, 7:34:38 PM6/30/18
to Jenkins Users
It will work if you use cygwin on Windows and connect the node through SSH.

Simon Richter

unread,
Jun 30, 2018, 8:14:13 PM6/30/18
to jenkins...@googlegroups.com
Hi,

On 01.07.2018 01:34, Sverre Moe wrote:

> It will work if you use cygwin on Windows and connect the node through SSH.

It also works if you have cygwin or mingw on the system path.

We use a setup where we mix sh and cmd invocations in the same build,
using the regular PKGBUILD to generate mingw binaries, and then
converting them to a standalone Windows installer with NullSoft installer.

So the behaviour as-is is good.

Simon

Francois Marot

unread,
Jul 3, 2018, 3:39:18 AM7/3/18
to Jenkins Users

FWIW, I made a simple function in my Jenkins pipeline shared library (see (1) or (2)) that I often use that is mostly cross compatible on windows/Linux.
It calls sh or bat dependeing on the OS, as long as the passed command is compatible with both.
But that is the case for most maven or git commands.
With this utility, my pipeline is fully compatible with both OS and each project can force it's execution on Linux or Windows, it works.


Here is the file      jenkins-shared-libraries/vars/batOrSh.groovy:

    // Simple utility whose goal is to have a common utility in Jenkins pipeline or script running both on windows or Linux
    def call(String command) {
        if (isUnix()) {
            sh command
        } else {
            bat command
        }
    }

    def call(Map args) {
        if (isUnix()) {
            sh args
        } else {
            bat args
        }
    }

    /** Sometimes we have to use different commands whether we are on Linux or Windows
     * For exemple:
     * git add *pom.xml on Windows
     * git add '*pom.xml' on Linux */
    def call(String commandLinux, String commandWindows) {
        if (isUnix()) {
            sh commandLinux
        } else {
            bat commandWindows
Reply all
Reply to author
Forward
0 new messages