DEV Community

Jesse Houwing for Xebia Microsoft Services

Posted on • Originally published at jessehouwing.net on

2

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.

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay