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

问题描述 投票:0回答:5

我目前正在尝试将 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...

观察到

> 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.
powershell path github-actions
5个回答
33
投票

出于安全原因,

add-path
set-env
命令已于 2020 年 10 月 1 日弃用:https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-添加路径命令/

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

假设您使用

Powershell
,默认 shell:

echo "C:\directory\to\add\to\path" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

或者

bash

echo "C:\directory\to\add\to\path" >> $GITHUB_PATH

13
投票

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

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

参见添加内容


4
投票

其他答案有点过时了(由于 GitHub Actions 不赞成添加路径,如 @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

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


1
投票

编辑:GitHub 已弃用此功能,请参阅其他答案...
参考:https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands/

您必须使用操作语法

echo "::add-path::..."
,根据您的情况:

...
    - 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 .;
        echo "::add-path::./swigwin-4.0.1"
    - name: Check swig
      run: swig -version

src:https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#adding-a-system-path


0
投票

这就是现在的方法

"/path/to/commands" >> $GITHUB_PATH

还可以使用

$HOME
作为
path/to/command
的一部分而不是绝对路径,以避免参考问题

https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#example-of-adding-a-system-path

© www.soinside.com 2019 - 2024. All rights reserved.