You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell与GitHub Actions中JSON解析差异及取值问题求助

解决GitHub Actions中PowerShell提取JSON属性无输出及设置环境变量的问题

1. 修正JSON属性访问语法

你的代码里$json.[some_property]是错误语法,正确的属性访问应该去掉属性名前的点,写成:

$json = Get-Content integration-manifest.json | ConvertFrom-Json
$json.some_property

如果属性名包含特殊字符(比如空格、连字符),需要用引号包裹:$json."some-property"

2. 确保在GitHub Actions中输出可见

在GitHub Actions的windows-latest运行器中,PowerShell默认可能不会自动输出变量值,你可以显式使用Write-Output强制输出:

$json = Get-Content integration-manifest.json | ConvertFrom-Json
Write-Output $json.some_property

3. 将属性值保存到GitHub Actions环境变量

要把提取的值存入环境变量供后续步骤使用,需要写入$env:GITHUB_ENV文件,方法如下:

$json = Get-Content integration-manifest.json | ConvertFrom-Json
$propertyValue = $json.some_property
# 将值写入GITHUB_ENV,后续步骤可通过env.SOME_PROPERTY访问
"some_property=$propertyValue" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8

后续步骤中,你可以用${{ env.some_property }}引用这个环境变量进行对比操作。

4. 排查运行器环境差异

如果仍无输出,检查以下几点:

  • 确认integration-manifest.json文件路径正确,在GitHub Actions中要确保文件已被检出到工作目录
  • 检查JSON文件格式是否合法,避免因解析失败导致无输出
  • 可以添加调试命令查看文件内容:Get-Content integration-manifest.json,确认文件内容符合预期

内容的提问来源于stack exchange,提问作者mikey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 17:27:15