Environment Variable setup from windows runner

Was getting warning for setting set-env in scripts. So was trying to change

run: |
echo "buildEnvironmentUrl=$($hashProperties.buildenvironmenturl)" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
echo "requestId=($hashProperties.requestid)" >> $GITHUB_ENV

and in next step, echo is printing null

run: |
echo "RequestID : $requestId"
echo "BuildEnvURL: $buildEnvironmentUrl"

In both ways I am not able to set the environment variables, Don’t know what’s missing. Checked I am able to print this with just echo “echo “buildEnvironmentUrl=$($hashProperties.buildenvironmenturl)””

and action runner is 2.273.5 and windows-2019.

in powershell we should use “$env:buildEnvironmentUrl” to access the variable.
Thanks @thboop

@navin22,

Different shells have different expressions to access the environment variables.
For example:

env:
  MSG: Hello
jobs:
  print_env_var:
    name: Print environment variable
    runs-on: windows-latest
    steps:
    - name: Print environment variable with cmd 
      run: echo "String matches %MSG%"
      shell: cmd

    - name: Print environment variable with powershell 
      run: echo "String matches $env:MSG"
      shell: powershell

    - name: Print environment variable with bash 
      run: echo "String matches $MSG"
      shell: bash

    - name: Print environment variable with pwsh 
      run: echo "String matches ${env:MSG}"  # Use $env:MSG also can work
      shell: pwsh

    - name: Print environment variable with python
      run: |
        import os
        print("String matches", os.environ['MSG'])
      shell: python

On Windows runners, the default shell is PowerShell.

1 Like

How do you pass an environment variable to the with for PowerShell?
The code for me isn’t working

- name: Checkout Repo
  uses: actions/checkout@v2
  with:
    repository: 'MyOrg/MyRepo'
    ref: ${env:CURRENT_BRANCH_PS}

You can’t use shell code in anything but run blocks. You can set an output with PowerShell though, and then refer to it in a workflow expression:

- name: Set output
  id: foo 
  run: |
    echo "::set-output name=branch::${Env:CURRENT_BRANCH_PS}"
- name: Use output
  uses: actions/checkout@v2
  with:
    repository: 'MyOrg/MyRepo'
    ref: ${{ steps.foo.outputs.branch }}
1 Like