Commit f1d01398 authored by Auto Backup's avatar Auto Backup

fix: 修复命令执行函数,增强跨平台兼容性,解决powershell语法问题

parent d3f046f9
// 新增带返回值的跨平台 PowerShell 执行函数 // 新增带返回值的跨平台命令执行函数
def runPowerShellWithOutput(script) { def runCommandWithOutput(script) {
try { try {
if (isUnix()) { if (isUnix()) {
return sh(script: "pwsh -Command \"${script}\"", returnStdout: true).trim() // Unix 系统使用 sh 执行命令
return sh(script: "${script}", returnStdout: true).trim()
} else { } else {
// Windows 系统,尝试多种方式
try {
// 方式1: 尝试使用 powershell 步骤 (需要 PowerShell 插件)
return powershell(script: script, returnStdout: true).trim() return powershell(script: script, returnStdout: true).trim()
} catch (Exception psError) {
echo "⚠️ PowerShell 步骤失败,尝试使用 bat 命令: ${psError.message}"
// 方式2: 使用 bat 命令执行 PowerShell
return bat(script: "powershell -Command \"${script}\"", returnStdout: true).trim()
}
} }
} catch (Exception e) { } catch (Exception e) {
echo "执行 PowerShell 脚本失败: ${e.message}" echo "执行命令失败: ${e.message}"
throw e throw e
} }
} }
pipeline { pipeline {
...@@ -34,7 +42,7 @@ pipeline { ...@@ -34,7 +42,7 @@ pipeline {
echo "🚀 开始运行环境检测..." echo "🚀 开始运行环境检测..."
// 使用新创建的环境检测脚本 // 使用新创建的环境检测脚本
def envCheckResult = runPowerShellWithOutput("node ./check-environment.js") def envCheckResult = runCommandWithOutput("node ./check-environment.js")
echo "环境检测结果:" echo "环境检测结果:"
echo envCheckResult echo envCheckResult
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment