Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 2
Message 1 of 2

How to run steps involving environment variables on both Linux and Windows

Solved! Go to Solution.
build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
          - ubuntu-latest
          - windows-latest

    steps:
    - uses: actions/checkout@master
    - uses: actions/setup-node@v1
      with:
        node-version: 12
    - name: Install dependencies
      run: yarn install --dev

    - name: Build binaries and upload to GitHub
      if: shell == 'bash'
      run: yarn prebuild --all -u $PREBUILD_TOKEN
      env:
        PREBUILD_TOKEN: ${{ secrets.PREBUILD_TOKEN }}
    - name: Build binaries and upload to GitHub
      if: shell == 'powershell'
      run: yarn prebuild --all -u ${env:PREBUILD_TOKEN}
      env:
        PREBUILD_TOKEN: ${{ secrets.PREBUILD_TOKEN }}

I have this job that is supposed to upload builds to GitHub releases. It runs on both Linux and Windows. As Powershell is used as a shell for Windows builds, I have to use a different syntax to read the environment variable that stores credentials to upload to GitHub. Otherwise, my Windows builds would just fail. So I tried to add conditional steps. But now GitHub doesn't run the action at all and instead errors with:

### ERRORED 13:33:33Z

- Your workflow file was invalid: The pipeline is not valid. .github/workflows/prebuild.yml (Line: 30, Col: 11): Unrecognized named-value: 'shell'. Located at position 1 within expression: shell == 'bash',.github/workflows/prebuild.yml (Line: 35, Col: 11): Unrecognized named-value: 'shell'. Located at position 1 within expression: shell == 'powershell'

What is the suggested way of running the same steps in different shells when access to environment variables is necessary? 

1 Reply
Solution
GitHub Partner
Message 2 of 2

Re: How to run steps involving environment variables on both Linux and Windows

‘shell’ is not recognized because it’s not the value of workflow context. As a workaround, you can use below condition instead.

Typically you can use the context value for the condition filter when access to environment variable is necessary. Please check the link here for more details.

 

if: runner.os == 'Linux'
… bash command
If: runner.os == 'Windows'
… powershelll command

or 

 

if: matrix.os == 'ubuntu-latest'
… bash command
If: matrix.os == 'windows-latest'
… powershelll command

context.png