如何确定Jenkins管道中的当前操作系统

Fed*_*deN 6 grails jenkins jenkins-pipeline

确定Jenkins管道正在运行的当前操作系统的方法是什么?

上下文:我正在构建一个共享的Jenkins管道脚本,该脚本应该在所有平台(windows,OSX,linux)上运行,并在每个平台上执行不同的操作.

我尝试过类似的东西:

import org.apache.commons.lang.SystemUtils

if (SystemUtils.IS_OS_WINDOWS){
   bat("Command")
}
if (SystemUtils.IS_OS_MAC){
   sh("Command")
}
if (SystemUtils.IS_OS_LINUX){
   sh("Command")
}
Run Code Online (Sandbox Code Playgroud)

但即使它在Windows或Mac上运行node它总是进入SystemUtils.IS_OS_LINUX分支

我试过像这样的快速管道.

node('windows ') {
     println ('## OS ' + System.properties['os.name'])
}
node('osx ') {
     println ('## OS ' + System.properties['os.name'])
}
node('linux') {
     println ('## OS ' + System.properties['os.name'])
}
Run Code Online (Sandbox Code Playgroud)

每个节点都在具有正确操作系统的机器中正确运行,但所有节点都打印出来 ## OS Linux

有任何想法吗?

谢谢联邦

fed*_*rzi 13

假设您将Windows作为唯一的非unix平台,您可以使用管道功能isUnix()uname检查您所在的Unix操作系统:

def checkOs(){
    if (isUnix()) {
        def uname = sh script: 'uname', returnStdout: true
        if (uname.startsWith("Darwin")) {
            return "Macos"
        }
        // Optionally add 'else if' for other Unix OS  
        else {
            return "Linux"
        }
    }
    else {
        return "Windows"
    }
}
Run Code Online (Sandbox Code Playgroud)


Jon*_*n S 11

据我所知,Jenkins只区分windows和unix,即如果在windows上,使用bat,在unix/mac/linux上,使用sh.所以,你可以使用isUnix(),这里更多信息,以确定如果你是在UNIX或Windows和UNIX系统中使用sh和@Spencer马龙的回答的情况下,以prope有关该系统的详细信息(如果需要).