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
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
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
- Install Conditional Build Step plugin
- You will get below option as Jenkins build step after installation of above plugin
- Add Conditional Build Step – Single or Multiple.
- In Run? Condition select “Execute Shell” and enter command
- Provide Linux Shell Commands as Steps to run if condition is met.
- Add another Conditional Build Step – Single or Multiple.
- In Run? Condition Select “Execute Windows Batch Commands” and enter command
- Provide Windows batch commands as Steps to run if condition is met.
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.