Mizux
Mizux

Reputation: 9309

How to update the PATH in a github action workflow file for a windows-latest hosted runner

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.

Upvotes: 30

Views: 33398

Answers (5)

Adibas03
Adibas03

Reputation: 430

This is the way to do this now
"/path/to/commands" >> $GITHUB_PATH

Also use $HOME as part of your path/to/command instead of an absolute path, to avoid reference issues

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

Upvotes: 2

Shi Chen
Shi Chen

Reputation: 331

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

Upvotes: 22

erb
erb

Reputation: 15551

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.

Upvotes: 6

Mizux
Mizux

Reputation: 9309

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

Upvotes: 0

Kel Solaar
Kel Solaar

Reputation: 4090

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

Upvotes: 38

Related Questions