15

I have been previously using set-output for setting values, but we now get thee "deprecated feature" messages and I'm using $GITHUB_OUTPUT as prescribed.

I replace all instances of

   run: echo ::set-output name=Key::Value

with

   run: "Key=Value" >> $GITHUB_OUTPUT

but Key does not appear to be set.

My runner is on Windows, version 2.299.1 and the workflow is using CMD. All calls to set-output work, and all using $GITHUB_OUTPUT do not.

Simplified action code

defaults:
run:
shell: cmd

jobs:
  EnvSetup:
    name: Publish Base Environment Vars
    runs-on: [self-hosted, Windows, myLabel]
    outputs:
      var_Project: ${{ steps.set-Project.outputs.Project }}
      var_Val1: ${{ steps.set-Val1.outputs.Val1 }}
      var_Val2: ${{ steps.set-Val2.outputs.Val2 }}
    steps:
      - name: Project
        id: set-Project
        run: echo ::set-output name=Project::Larry

      - name: Val1
        id: set-Val1
        run: echo "Val1=Curly" >> $GITHUB_OUTPUT

      - name: Val2
        id: set-Val2
        run: echo "Val2=Moe" >> $GITHUB_OUTPUT

...

  Testing:
    name: ShowStuff
    runs-on: [self-hosted, Windows, myLabel]
    needs: [EnvSetup]
    env:
      MyProject: ${{ needs.EnvSetup.outputs.var_Project }}_ABC
    steps:
      - name: Print environment variables
        run: |
          echo "Project: ${{ needs.EnvSetup.outputs.var_Project }}" ^
          echo "MyProject: ${{ env.MyProject }}" ^
          echo "Val1: ${{ needs.EnvSetup.outputs.var_Val1 }}" ^
          echo "Val2: ${{ needs.EnvSetup.outputs.var_Val2 }}"

The output:

echo "Project: Larry"
echo "MyProject: Larry_ABC"
echo "Val1: "
echo "Val2: "

From everything I've seen, the way to reference the values hasn't changed, just the set.

Has anyone else tried it using CMD? I'll go to PowerShell if I have to, but that's not a small change if I can avoid it.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
Mr Bigglesw0rth
  • 165
  • 1
  • 6
  • Syntax looks correct (compare to this example using linux / bash https://stackoverflow.com/a/74378072/3302747). So I'm guessing it's something with your runner (windows). Try something simple, like echo out Path. I'm not 100% sure of the windows syntax for cmd – Baked Inhalf Nov 15 '22 at 12:25
  • @BakedInhalf, the windows runner uses PowerShell Core as the default script shell. Linux and Mac use bash. When targeting multiple platforms, it makes sense to explicitly state the `shell: cmd | pwsh | powershell | bash`. – jessehouwing Nov 15 '22 at 16:39
  • 1
    @jessehouwing No need to specify shell every time, just add it in ```defaults: run: shell: ``` (needs formatting) – Baked Inhalf Nov 16 '22 at 07:43
  • You dropped the `echo`. – Matthew Read May 30 '23 at 16:22

1 Answers1

24

Windows runs the script task using PowerShell Core by default, not bash. So you need to use PowerShell syntax, or set the shell: bash property on the script action.

  - name: Val2
    id: set-Val2
    run: echo "Val2=Moe" >> $GITHUB_OUTPUT
    shell: bash

When using these commands with PowerShell, make sure you redirect to $env:GITHUB_OUTPUT:

  - name: Val2
    id: set-Val2
    run: echo "Val2=Moe" >> $env:GITHUB_OUTPUT
    shell: pwsh

I also explicitly added shell: pwsh above, as the "old PowerShell" needs to be told to write UTF-8:

  - shell: powershell
    run: |
      "mypath" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append

When using shell: cmd you'd need to use %GITHUB_OUTPUT%, and change the codepage to Unicode:

@chcp 65001>nul
echo Val2=Moe >> %GITHUB_OUTPUT%

enter image description here

IGx89
  • 872
  • 7
  • 18
jessehouwing
  • 106,458
  • 22
  • 256
  • 341