How to identify Jenkins Slave Operating System in a Build

Jenkins has very useful build step of executing Windows batch or Linux Shell command. User has option to choose Jenkins slave by restricting job to run on slave with specific condition. Jenkins slave may be either Windows or Linux client. We have a way to find Operating Systems of Jenkins slave machine.

While using Jenkins with Windows and Linux slave and Jenkins’ build step as Windows batch or Linux shell commands, Major challenge in job configuration is random slave choice and hence random operating systems of slave.

We know that any Jenkins job having Linux shell command won’t run on Windows slave and below error usually occurs.

[workspace] $ sh -xe C:\Windows\TEMP\hudson8410636081882452618.sh
The system cannot find the file specified
FATAL: command execution failed

How to identify Jenkins Slave Operating System in a Build
In the same way any Jenkins job having Windows batch command won’t run on Linux slave and below error message is displayed.

[SLAVE OS] $ cmd /c call /tmp/hudson1452251523978015579.bat
FATAL: command execution failed
java.io.IOException: Cannot run program "cmd" (in directory "/jenkins/workspace/SLAVE OS"): error=2, No such file or directory

jenkins_batch_on_linux

 

How to Make Jenkins Build Step Independent of Jenkins Slave Operating Systems

Jenkins has huge repository of open source plugins and one of them is “Conditional Build Step“. Click here to Download. We can take help of this plugin and find slave operating systems of Jenkins slave. Once we have Jenkins slave then we can make ongoing build steps compatible to Jenkins OS.

Steps to know Jenkins Slave OS

  1. Install Conditional Build Step plugin
  2. You will get below option as Jenkins build step after installation of above plugin
    Add Conditional Build Step
  3. Add Conditional Build Step – Single or Multiple.
  4. In Run? Condition select “Execute Shell” and enter command
     echo "IsShell"
  5. Provide Linux Shell Commands as Steps to run if condition is met.
  6. Add another Conditional Build Step – Single or Multiple.
  7. In Run? Condition Select “Execute Windows Batch Commands” and enter command
  8. Provide Windows batch commands as Steps to run if condition is met.
     echo "IsWindows"

Jenkins_Slave_OS_Conditional_Build

Above job will be able to run on any – Windows or Linux slave without showing above errors.

Let me know if you face any difficulties in configuration of the same, I will be happy to help you guys.

You may also like...

3 Responses

  1. Satish says:

    Hi, thanks for the suggestions in the post. Although it works, the console output will be a bit messy if you choose to run a shell command & windows command in the conditions (With Jenkins ignoring the failed method). Alternate method which worked for me & looked a bit cleaner is choosing the node option in the Step condition. If you have multiple nodes with the same OS, then you can multi select the right nodes.

    • Chintan Mehta says:

      Hi, Yes you are right but here my concern is I have one jenkins job which I am not sure on which OS i will run. Depending on scenarios I may run it on Windows or Linux. I didn’t want to change anything on job so above option will automatically handle OS and execute appropriate shell commands or windows command.
      If you have got what I mean and have some good options plz suggest so I can share.
      Thanks

  2. Ivan says:

    Hi,

    I had the same problem and I just fixed it doing a ‘strings match’ on the environment variables for the logged-in user name.

    Linux:
    String1: ${ENV,var=”USER”}
    String2: jenkins

    Windows:
    String1: ${ENV,var=”USERNAME”}
    String2: jenkins

    I hope it helps.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!