Search code examples
bashgithub-actions

Why another echo is needed in custom github action output


From the documentation of github actions:

outputs:
random-number:
  description: "Random number"
  value: ${{ steps.random-number-generator.outputs.random-id }}
runs:
  using: "composite"
  steps:
    - id: random-number-generator
    run: echo "::set-output name=random-id::$(echo $RANDOM)"
    shell: bash

In the run clause 2 echos exist, one for the whole line and one echo $RANDOM

Isn't the second echo unnessecary?

On my computer

echo "::set-output name=random-id::$(echo $RANDOM)"

outputs:

::set-output name=random-id::28124

while

echo "::set-output name=random-id::$RANDOM"

outputs

::set-output name=random-id::28123

so both works.

So why is $(echo $RANDOM) needed ?


Solution

  • This comes from Metadata syntax / outputs for composite actions and is not strictly needed.

    The documentation mentions:

    If you need to pass environment variables into an action, make sure your action runs a command shell to perform variable substitution.

    By using $(echo $RANDOM), you "force" said variable substitution.