DEV Community

Jesse Houwing for Xebia Microsoft Services

Posted on • Originally published at jessehouwing.net on

Issuing workflow commands from the Windows shell in GitHub Actions

Issuing workflow commands from the Windows shell in GitHub Actions

It's a little-known fact that the default shell in for GitHub Actions is different depending on the operating system on which you run. And that the syntax to set a variable, for example, differs too.

The docs explain the syntax for Bash and PowerShell:
Workflow commands for GitHub Actions - GitHub Docs

Below is an example of the different syntax required to set an output variable in each of the most common shells:

jobs:
  windows:
    runs-on: windows-latest
    steps:
    # Runs on PowerShell Core
    - name: Set an output variable on Windows
      run: |
        echo foo=bar >> $env:GITHUB_OUTPUT

  linux:
    runs-on: ubuntu-latest
    steps:
    # Runs on Bash
    - name: Set an output variable on Linux
      run: |
        echo foo=bar >> $GITHUB_OUTPUT

Enter fullscreen mode Exit fullscreen mode

When you always target the same OS, it won't matter much, but if you have multi-platform jobs, matrix jobs, reusable templates, and composite actions, it pays to be explicit. That way your bash script won't suddenly be executed by PowerShell or vice versa.

You aren't limited to pwsh on windows or bash on linux and mac either. As long as they are installed on the Runner, you can run either on each OS.

jobs:
  windows:
    runs-on: windows-latest
    steps:
    # Runs on Bash
    - name: Set an output variable on Windows
      run: |
        echo foo=bar >> $GITHUB_OUTPUT
      shell: bash
    - name: Set an output variable on Windows
      run: |
        echo foo=bar >> $env:GITHUB_OUTPUT
      shell: pwsh
    - name: Set an output variable on Windows
      # Requires explicitly setting the output encoding
      run: |
        "foo=bar" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append
      shell: powershell


  linux:
    runs-on: ubuntu-latest
    steps:
    # Runs on Bash
    - name: Set an output variable on Linux
      run: |
        echo foo=bar >> $GITHUB_OUTPUT
    - name: Set an output variable on Linux
      run: |
        echo foo=bar >> $env:GITHUB_OUTPUT
      shell: pwsh
Enter fullscreen mode Exit fullscreen mode

Today someone asked what the correct syntax would be to do this on the shell: cmd old Windows Command Shell. I couldn't find that in the docs.

To be honest, since I wrote half of that doc, I was certain I didn't add that information...

But after some experimentation I found out you need to do the same things you'd do for the old PowerShell. Set the encoding to utf-8 and use the correct environment variable syntax:

jobs:
  windows:
    runs-on: windows-latest
    steps:
    - shell: cmd
      run: |
        @chcp 65001>nul
        echo foo=bar >> %GITHUB_OUTPUT%
Enter fullscreen mode Exit fullscreen mode

First published here.

Top comments (0)