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

查看:29
本文介绍了如何确定 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

有什么想法吗?

谢谢费德

推荐答案

据我所知 Jenkins 只区分 windows 和 unix,即如果在 windows 上使用 bat,在 unix/mac/linux 上使用 sh.所以你可以使用 isUn​​ix(), 更多信息在这里,以确定您是在 unix 还是 windows 上,在 unix 的情况下使用 sh 和 @Spencer Malone's回答以提供有关该系统的更多信息(如果需要).

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天全站免登陆