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

Git短SHA在Linux/Mac生效但Windows失效,Github Actions配置求助

问题原因与解决方案

原因

你当前的命令基于Bash(Linux/macOS默认Shell)语法,而GitHub Actions的Windows Runner默认使用PowerShell(pwsh),两者在环境变量引用、重定向逻辑上存在差异:

  • PowerShell中引用环境变量需用$env:变量名,而非Bash的$变量名,因此$GITHUB_ENV在PowerShell里无法正确解析为目标文件路径
  • 虽然PowerShell也支持$(...)执行子命令,但因重定向目标错误,导致内容无法写入正确的GITHUB_ENV文件

解决方案1:强制用Bash执行(跨系统通用)

在Step中指定shell: bash,Windows Runner自带Git Bash,可直接运行原命令:

- name: Set SHORT Sha
  run: echo "SHORT_SHA=$(git rev-parse --short ${{ github.sha }})" >> $GITHUB_ENV
  shell: bash

解决方案2:适配PowerShell语法

如果需要用PowerShell执行,调整为Windows兼容的写法:

- name: Set SHORT Sha
  run: |
    $shortSha = git rev-parse --short ${{ github.sha }}
    "SHORT_SHA=$shortSha" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
  shell: pwsh

或者更简洁的单行写法:

- name: Set SHORT Sha
  run: "SHORT_SHA=$(git rev-parse --short ${{ github.sha }})" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
  shell: pwsh

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 09:20:21