Github secrets not found windows-latest / windows-2019

Followling investigation I have to conclude that currently secrets don’t work within the windows-latest or windows-2019 frameworks.

I have two workflows on the same repository. One uses ubuntu-latest and the other windows-latest. They should have the same visibility. When I replace the username and password on the code below with literal text it works. Secrets are not functional within the windows environment. If there is something I am missing please let me know.

- name: Docker login
env:
DockerUserName: ${{ secrets.DockerHubUser }}
DockerPassword: ${{ secrets.DockerHubPassword }}
run: |
echo ‘Docker login’
set h+

echo “$DockerPassword” | docker login -u “$DockerUserName” --password-stdin index.docker.io

Solution was to use:

echo “$env:DockerPassword” | docker login -u “$env:DockerUserName” --password-stdin index.docker.io
Seems the envirnomental vars are handled slightly differently.
Thank you @ericsciple for your help.

Hi @naj8ry , 

Yeah, you are right. In windows runner, the default shell for runs is powershell. In powershell script, you need to use $env:var_name syntax to invoke envirnoment variable. 

There is the document for using a specific shell: https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#using-a-specific-shell 

1 Like

@naj8ry ,

Yeah, when you want to access the environment variables in the command lines, the expression syntaxes are different on different shells.

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

However, if the environment variables are defined by the following ways,

  1. using the " env" key to define the custom environment variables in the workflow

  2. using the Workflow command (::set-env name={name}::{value}) to set environment variables in the workflow

the environment variables will be added to the env context, then you can use the expression syntax " ${{ env.<env name> }}" to access the environment variables. This expression syntax almost can be available to all the shells.

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 ${{ env.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 ${{ env.MSG }}"
      shell: bash

    - name: Print environment variable with pwsh
      run: echo "String matches ${{ env.MSG }}"
      shell: pwsh

    - name: Print environment variable with python
      run: print("String matches", "${{ env.MSG }}")
      shell: python

That is great guys,

Thank you for your help.

@brightran Is is possible to add this to the documentation on environmental variables? 

I did read: https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables

having this as an appendix might save someone else some time.

Thanks again,

Nicholas