Mr Bigglesw0rth
Mr Bigglesw0rth

Reputation: 215

Value not set using $GITHUB_OUTPUT

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.

Upvotes: 20

Views: 15557

Answers (2)

jessehouwing
jessehouwing

Reputation: 114751

The official docs have since been updated, but you need to switch to "PowerShell" at the top of the page to see all the examples in PowerShell syntax:

Screenshot

If you're looking for the Windows Shell Syntax to issue GitHub Actions commands, I've written a blog to cover that syntax.


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
  shell: bash
  run: echo "Val2=Moe" >> $GITHUB_OUTPUT
  

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

Note: You can omit echo if you do not want to see the output being printed. Ex: run: "Val2=Moe" >> $env:GITHUB_OUTPUT works too.

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%

Output Screenshot

Upvotes: 41

CaMiX
CaMiX

Reputation: 750

The accepted answer for shell pwsh did not work for me I had to use the following Append method in this example in order to get it to work:

- name: Set color
    id: color-selector
    run: |
        "SELECTED_COLOR=green" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8
  - name: Get color
    env:
      SELECTED_COLOR: ${{ steps.color-selector.outputs.SELECTED_COLOR }}
    run: Write-Output "The selected color is $env:SELECTED_COLOR"

Upvotes: 1

Related Questions