23

I'm currently trying to add GitHub actions workflow to a repo...

To do a C++/CMake/swig/python development (i.e. native python library dev), I need to download and install swigwin and have it available in the PATH...

Unfortunately it seems the $env:Path... command is not take into account during the next subsequent steps

Example

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...

Observed

> 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.
2
  • Does anyone know how to do it in ubuntu-latest instead of windows-latest?
    – Pedro A
    Oct 13, 2022 at 3:38
  • For linux, just run: echo "/usr/path/whatever" >> $GITHUB_PATH in a step, and the path will be updated for all subsequent steps.
    – wolf2600
    Oct 14, 2022 at 14:54

4 Answers 4

31

The add-path and set-env commands have been deprecated the 1st October 2020 for security reasons: https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands/

The recommended way to add to %PATH% is using environment files as follows:

Assuming you use Powershell, the default shell:

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

or alternatively for bash:

echo "C:\directory\to\add\to\path" >> $GITHUB_PATH
1
  • Does not work for me on the self hosted windows github runner.
    – AlexS
    Mar 22 at 8:16
13

There is also a shorter way to achieve this in Powershell, which is the default shell that windows hosted runner uses:

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

See Add-Content

1
4

The other answers are a bit out of date (due to GitHub Actions deprecating add-path as explained in @Kel Solaar's answer), here's a full example based on @Mizux answer:

    - 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

Another difference to @Mizus answer is that the absolute path to the swig directory is used, this is to ensure it still works even though the working directory changes.

1
  • Thanks! This worked to allow me to use Cppcheck in other steps after installing with choco echo "$env:ProgramFiles\Cppcheck" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append May 21, 2022 at 6:45
1

EDIT: GitHub have deprecated this, please see other answer...
ref: https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands/

You must use the action syntax echo "::add-path::...", in your case:

...
    - 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

2
  • the link is not valid anymore
    – jitter
    Apr 9, 2020 at 18:42
  • 1
    @jitter Link fixed ! seems Github doc writters follow my advice and reorganize the doc accordingly :D
    – Mizux
    Apr 9, 2020 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.