GitHub Actions Windows pwsh环境运行gitversion.exe出现StandardOutputEncoding报错如何解决
错误原因
该报错是Windows平台PowerShell 7+调用特定控制台程序时的已知兼容性问题:GitVersion为代表的部分.NET编写的控制台应用会直接调用底层Windows控制台接口输出内容,未遵循标准输出流的编码约定,当PowerShell尝试直接重定向捕获这类程序的输出时,会触发StandardOutputEncoding校验失败。本地运行正常是因为本地PowerShell的控制台配置、编码设置或GitVersion版本与GitHub Actions运行器的默认环境存在差异。
可行解决方案
- 方案1:使用
Start-Process明确重定向输出后再读取,兼容性最强
# 启动GitVersion进程,将标准输出重定向到临时文件 Start-Process -FilePath .\gitversion.exe -NoNewWindow -Wait -PassThru -RedirectStandardOutput gitversion_output.json -RedirectStandardError gitversion_error.log | Out-Null # 读取输出文件转换为JSON对象 $versionInfo = Get-Content gitversion_output.json -Raw | ConvertFrom-Json # 清理临时文件 Remove-Item gitversion_output.json, gitversion_error.log -ErrorAction SilentlyContinue
- 方案2:通过cmd中转调用,绕开PowerShell的直接输出捕获逻辑
$versionInfo = (cmd /c ".\gitversion.exe 2>&1") | ConvertFrom-Json
- 方案3:调用前强制设置控制台输出编码适配GitVersion的输出规则
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8 $versionInfo = (& .\gitversion.exe) | ConvertFrom-Json
内容的提问来源于stack exchange,提问作者jessehouwing
相关产品推荐
相关产品推荐

