Piotr Stachyra
Piotr Stachyra

Reputation: 81

Can Jenkins job execute shell or Windows command conditionally based on agent OS?

I'm trying to build a job that will execute a program on remote agents. The program is either exe when agent is on Windows or shell script when it is on Linux. The program is already deployed on agent machines. I'm looking for a solution that would allow me to conditionally run either exe (Execute Windows batch command) or shell (Execute shell) depending on the agent's OS.

So far I found there is a run condition plugin (https://wiki.jenkins-ci.org/display/JENKINS/Run+Condition+Plugin), whuch would be great, unfortunatelly it does not have check for agent's OS.

Is there any way to accomplish that? Google tells me nothing.

Upvotes: 5

Views: 6871

Answers (2)

twasbrillig
twasbrillig

Reputation: 18831

The solution that I found that avoids any exceptions in the output is:

For Windows, compare that these strings match:

${ENV,var="OS"}
Windows_NT

For Linux, compare that these strings match:

${ENV,var="OS"}

(The second string is left blank.)

Upvotes: 1

Piotr Stachyra
Piotr Stachyra

Reputation: 81

Found something that seems to work:

https://www.techiepie.com/2016/04/14/how-to-identify-jenkins-slave-operating-system-in-a-build/

  • Add Conditional Build Step – Single or Multiple.
  • In Run? Condition select “Execute Shell” and enter command echo "IsShell"
  • 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 echo "IsWindows"
  • Provide Windows batch commands as Steps to run if condition is met.

Upvotes: 3

Related Questions