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

如何在Windows版Github Actions中改用Cygwin Bash而非Git Bash?

问题

我在Windows环境的GitHub Actions中使用windows-2022镜像,已完成Cygwin安装,但该镜像默认预装Git Bash。当步骤指定shell: bash时,实际调用的是Git Bash(版本为GNU bash, version 5.2.12(1)-release (x86_64-pc-msys)),而非Cygwin的Bash(路径C:\cygwin64\bin\bash.exe,版本GNU bash, version 4.4.12(3)-release (x86_64-unknown-cygwin)),导致无法运行Cygwin工具。如何让GHA的特定步骤或所有步骤使用Cygwin的Bash?

我的Cygwin安装步骤如下:

- name: "Windows: Cygwin download"
  if: runner.os == 'Windows'
  run: |
    Invoke-WebRequest 'https://cygwin.com/setup-x86_64.exe' -OutFile 'setup-x86_64.exe'

- name: "Windows: Cygwin setup"
  if: runner.os == 'Windows'
  # The setup does not complete properly in powershell for some reason
  shell: cmd
  run: .\setup-x86_64.exe --quiet-mode --site http://cygwin.mirror.constant.com --symlink-type=sys --packages mingw64-i686-binutils=2.37-2,mingw64-x86_64-binutils=2.37-2,curl,diffutils,git,m4,make,mercurial,mingw64-i686-gcc-core,mingw64-i686-gcc-g++,mingw64-x86_64-gcc-core,mingw64-x86_64-gcc-g++,patch,perl,rsync,unzip
解决方法

针对特定步骤使用Cygwin Bash

直接在目标步骤的shell参数中指定Cygwin Bash的完整路径,并配置必要的启动参数,确保加载Cygwin环境:

- name: 用Cygwin Bash执行命令
  if: runner.os == 'Windows'
  shell: C:\cygwin64\bin\bash.exe --login -o igncr -e -uo pipefail {0}
  run: |
    bash --version
    # 此处可执行依赖Cygwin工具的命令,例如make、gcc等

参数说明:

  • --login:加载Cygwin的环境配置,确保所有Cygwin工具可被找到
  • -o igncr:忽略Windows换行符(CR),避免脚本执行时出现格式错误
  • -e:命令执行失败时立即退出流程
  • -uo pipefail:管道中任意命令失败则整个管道标记为失败

全局设置默认使用Cygwin Bash

若希望所有步骤默认调用Cygwin Bash,可在工作流开头添加步骤,将Cygwin的bin目录追加到PATH环境变量最前端,覆盖Git Bash的路径优先级:

- name: 全局配置Cygwin Bash为默认Shell
  if: runner.os == 'Windows'
  run: |
    echo "C:\cygwin64\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

完成后,后续步骤指定shell: bash时,系统会优先调用Cygwin的Bash。可通过以下步骤验证:

- name: 验证默认Bash版本
  shell: bash
  run: bash --version

输出应显示Cygwin的Bash版本信息。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 09:31:01