Search This Blog

Breaking

Wednesday, 12 May 2021

[Jenkins] How to run same Jenkinsfile for Windows and Mac (Unix/Linux) Machine?

Recently I came across to a problem where I have to run the same Jenkins File which I was running in Linux (Mac) machine to the other Windows machine as well. The first thing which came in my mind (I believe will come to everyone's mind too) is to identify the machine OS and run the script accordingly.

While googling it's solution, reading stack overflow answers, Jenkins documentations, I came across to a solution, which helped me to put a condition in my Jenkins file based on the OS.

Jenkin provides a method isUnix() to check the os Type. So if your code is running in Unix/Linux or mac machine, you can put a condition like this:






 if(isUnix()) {

sh "mvn clean test" // Linux related stuff

} else {

bat "mvn clean test" // Windows related stuff

}

With this way, you can put the windows and Linux configurations separately in your Jenkins file.




No comments:

Post a Comment