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,确保路径准确即可。
排查验证步骤
- 在自托管运行器上手动执行
pwsh --version或pwsh7 --version,确认哪个命令能正确返回PowerShell 7的版本信息。 - 检查对应可执行文件是否存在于系统PATH环境变量中,若不在则使用绝对路径配置Shell。
内容的提问来源于stack exchange,提问作者Rebekka Bratlie
相关产品推荐
相关产品推荐

