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

GitHub Actions工作流中如何指定使用PowerShell 7替代默认PowerShell 5?

解决GitHub Actions自托管运行器指定PowerShell 7的配置问题

问题原因

你设置的shell: pwsh7不符合GitHub Actions的shell命名规范,官方并未将pwsh7作为PowerShell 7的默认识别别名,导致工作流无法找到对应的Shell执行环境。

正确配置方案

方案1:使用官方标准Shell名称

GitHub Actions中,PowerShell 7对应的标准Shell标识符是pwsh,这是PowerShell 7可执行文件的默认名称(Windows上为pwsh.exe,Linux/macOS上为pwsh)。修改后的工作流YAML如下:

name: Powershell script
on:
  push:
    branches:
      - master

jobs:
  myJob:
    name: PS - Update list
    runs-on: self-hosted

    steps:
      - name: Check out repository code
        uses: actions/checkout@v3
        with:
          fetch-depth: 1
      
      - name: Run PowerShell script
        shell: pwsh
        env:
          Creds: ${{ secrets.Creds }}
        run: | 
          ./Something.ps1

方案2:自定义Shell路径(适用于可执行文件名为pwsh7的场景)

如果你的自托管运行器上PowerShell 7的可执行文件确实被命名为pwsh7(比如自定义了别名或安装路径特殊),可以通过指定完整调用格式来配置:

Windows自托管运行器示例

- name: Run PowerShell script
  shell: 'pwsh7 {0}'
  env:
    Creds: ${{ secrets.Creds }}
  run: | 
    ./Something.ps1

Linux/macOS自托管运行器示例

- name: Run PowerShell script
  shell: '/usr/bin/pwsh7 {0}'
  env:
    Creds: ${{ secrets.Creds }}
  run: | 
    ./Something.ps1

也可以直接使用可执行文件的绝对路径,比如Windows上的C:\Program Files\PowerShell\7\pwsh.exe,确保路径准确即可。

排查验证步骤

  1. 在自托管运行器上手动执行pwsh --version或pwsh7 --version,确认哪个命令能正确返回PowerShell 7的版本信息。
  2. 检查对应可执行文件是否存在于系统PATH环境变量中,若不在则使用绝对路径配置Shell。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 09:02:38