如何确定Jenkins管道中的当前操作系统 [英] How to determine the current operating system in a Jenkins pipeline

查看:395
本文介绍了如何确定Jenkins管道中的当前操作系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What would be the way to determine the current OS a Jenkins pipeline is running?

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

Context: I'm building a shared Jenkins pipeline script that should run on all platforms (windows, OSX, linux) and execute something different in each platform.

我尝试过类似的事情:

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")
}

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

But even it is running on windows or mac node it always goes into the SystemUtils.IS_OS_LINUX branch

我尝试了这样的快速管道.

I tried a quick pipeline like this.

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

每个节点都可以在具有正确操作系统的机器上正确运行,但是所有节点都可以打印## OS Linux

Each node get correctly run in a machine with the correct OS but all of them print ## OS Linux

有什么想法吗?

谢谢 费德

推荐答案

据我所知,詹金斯只区分Windows和Unix,即如果在Windows上使用bat,在Unix/mac/linux上使用sh.因此,您可以使用isUnix()

As far as I know Jenkins only differentiates between windows and unix, i.e. if on windows, use bat, on unix/mac/linux, use sh. So you could use isUnix(), more info here, to determine if you're on unix or windows, and in the case of unix use sh and @Spencer Malone's answer to prope more information about that system (if needed).

这篇关于如何确定Jenkins管道中的当前操作系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆