自托管Windows GitHub Action Runner无法更新GITHUB_PATH问题
问题描述
使用以下GitHub Actions工作流时,在官方Windows Runner上运行正常,但在基于Windows Server 2022核心版自定义镜像搭建的自托管Runner(版本2.303.0,系统为Windows Server 2022数据中心版)上执行失败:
name: Test set path on: workflow_dispatch: jobs: test-cross-aws: runs-on: [self-hosted, '${{ matrix.platform.os }}', x64, '${{ matrix.platform.label }}'] #runs-on: windows-latest strategy: matrix: platform: [{ os: windows, label: wix-aws-windows-test }] steps: - name: create fake batchfile if: always() run: | mkdir testfolder echo "@echo off" | Out-File -FilePath testfolder/testbatch.bat -Encoding utf8 echo "echo SOMETHING" | Out-File -FilePath testfolder/testbatch.bat -Encoding utf8 - name: add path if: always() run: echo "$pwd\testfolder;" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append - name: run custom binary with path if: always() run: testbatch.bat
报错信息:
testbatch.bat : The term 'testbatch.bat' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\actions-runner\_work\_temp\744d9b45-8f99-4db5-ba7d-b95aad4dde97.ps1:2 char:1 + testbatch.bat + ~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (testbatch.bat:String) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : CommandNotFoundException Error: Process completed with exit code 1.
解决方案
针对自托管Windows Runner上的这个问题,可以尝试以下几种修复方式:
1. 显式通过CMD运行批处理文件
PowerShell环境下直接调用.bat文件会优先查找cmdlet而非可执行文件,显式调用CMD执行批处理可绕过该逻辑:
- name: run custom binary with path if: always() run: cmd /c testbatch.bat
2. 用可靠方式添加路径到GITHUB_PATH
手动拼接路径易出现格式问题,改用Join-Path构造路径,并用Add-Content追加内容(比Out-File -Append更稳定),同时用$env:GITHUB_WORKSPACE替代$pwd确保路径指向正确工作目录:
- name: add path if: always() run: | $targetPath = Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath "testfolder" Add-Content -Path $env:GITHUB_PATH -Value $targetPath -Encoding utf8
3. 验证并修复PATHEXT环境变量
Windows的PATHEXT变量决定系统识别的可执行文件扩展名,若自托管Runner的PATHEXT缺失.BAT,即使路径正确也无法直接运行批处理。先检查变量:
- name: Check PATHEXT run: Write-Output $env:PATHEXT
若缺少.BAT,可在工作流中追加该扩展名:
- name: Add .BAT to PATHEXT run: | $updatedPathext = "$env:PATHEXT;.BAT" echo "PATHEXT=$updatedPathext" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
4. 验证路径是否正确添加
在添加路径后增加步骤,确认目标目录已加入系统PATH:
- name: Verify PATH includes testfolder run: Write-Output $env:PATH | Select-String -Pattern "testfolder"
内容的提问来源于stack exchange,提问作者AlexS
相关产品推荐
相关产品推荐

