如何更新 Windows 最新托管运行程序的 github 操作工作流文件中的 PATH

Miz*_*zux 8 powershell path github-actions

我目前正在尝试将 GitHub 操作工作流添加到存储库...

要进行 C++/CMake/swig/python 开发(即本机 python 库开发),我需要下载并安装 swigwin 并使其在PATH...

不幸的$env:Path...是,在接下来的后续步骤中似乎没有考虑该命令

例子

name: Python Windows CI

on: [push, pull_request]

jobs:
  # Building using the GitHub runner environment directly.
  build:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v2
    - name: Check cmake
      run: cmake --version
    - name: Install swig
      run: |
        (New-Object System.Net.WebClient).DownloadFile("http://prdownloads.sourceforge.net/swig/swigwin-4.0.1.zip","swigwin-4.0.1.zip");
        Expand-Archive .\swigwin-4.0.1.zip .;
        $env:Path += ";.\swigwin-4.0.1";
        swig -version;
    - name: Check swig
      run: swig -version # swig cmdlet not found...

Run Code Online (Sandbox Code Playgroud)

观察到的

> Set up job
> Run actions/checkout@v23s
> Check cmake
v Install swig
...
SWIG Version 4.0.1
...
v Check swig
 swig -version
  shell: C:\Program Files\PowerShell\6\pwsh.EXE -command ". '{0}'"
swig : The term 'swig' 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 D:\a\_temp\0a8dc0e1-ec51-429b-abd0-cb3597e983ac.ps1:2 char:1
+ swig -version
+ ~~~~
+ CategoryInfo          : ObjectNotFound: (swig:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : CommandNotFoundException

##[error]Process completed with exit code 1.
Run Code Online (Sandbox Code Playgroud)

小智 13

在 Powershell 中还有一种更短的方法可以实现此目的,这是 Windows 托管运行程序使用的默认 shell:

Add-Content $env:GITHUB_PATH "C:\directory\to\add\to\path"

请参阅添加内容

  • 谢谢,在我找到这个之前浪费了很多时间。我不敢相信这个或更长的版本不在 https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-system-path (2认同)

Kel*_*aar 11

出于安全原因,add-path和set-env命令已于 2020 年 10 月 1 日弃用:https : //github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands/

添加到 %PATH% 的推荐方法是使用环境文件,如下所示:

假设您使用Powershell,默认外壳:

echo "C:\directory\to\add\to\path" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
Run Code Online (Sandbox Code Playgroud)

或替代为bash:

echo "C:\directory\to\add\to\path" >> $GITHUB_PATH
Run Code Online (Sandbox Code Playgroud)


erb*_*erb 4

其他答案有点过时了(由于 GitHub Actions 弃用了 add-path,如 @Kel Solaar 的答案中所述),这是基于 @Mizux 答案的完整示例:

    - name: Install swig
      if: "startsWith(runner.os, 'windows')"
      run: |
        (New-Object System.Net.WebClient).DownloadFile("http://prdownloads.sourceforge.net/swig/swigwin-4.0.1.zip","swigwin-4.0.1.zip");
        Expand-Archive .\swigwin-4.0.1.zip .;
        echo "$((Get-Item .).FullName)/swigwin-4.0.1" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
Run Code Online (Sandbox Code Playgroud)

与 @Mizus 答案的另一个区别是使用 swig 目录的绝对路径,这是为了确保即使工作目录发生变化它仍然可以工作。