AppVeyor构建中如何用命令行区分Linux与macOS运行环境?
AppVeyor 区分Linux与macOS构建环境的可行方案
方案1:使用uname命令判断(无额外依赖,最推荐)
Linux和macOS环境默认都自带uname工具,直接读取输出即可快速判断系统类型,不需要依赖Python等额外环境:
- 在shell脚本块中使用示例:
# 读取系统标识 os_type=$(uname -s) if [ "$os_type" = "Linux" ]; then # 此处填写Linux环境专属执行命令 apt update && apt install -y your-package elif [ "$os_type" = "Darwin" ]; then # 此处填写macOS环境专属执行命令 brew install your-package fi
- 直接写在
appveyor.yml单行命令的示例:
sh: 'if [ "$(uname -s)" = "Darwin" ]; then brew install cmake; else apt install -y cmake; fi'
方案2:使用你提到的Python方案获取判断结果
如果你的构建环境已经默认安装Python,也可以把Python命令的执行结果赋值给变量后做判断,写法如下:
os_type=$(python -c "import platform; print(platform.system())") if [ "$os_type" = "Linux" ]; then # Linux 专属执行逻辑 elif [ "$os_type" = "Darwin" ]; then # macOS 专属执行逻辑 fi
该方案的判断结果和uname方案完全一致,适合已经有Python依赖的项目使用。
方案3:通过AppVeyor内置环境变量判断
如果你在appveyor.yml中为不同操作系统指定了固定的构建镜像名,也可以直接读取APPVEYOR_BUILD_WORKER_IMAGE内置变量判断,比如你配置的镜像为Ubuntu2204、macOS-Ventura时,直接匹配变量值即可。
内容的提问来源于stack exchange,提问作者servoz
相关产品推荐
相关产品推荐

