github-actionsbuilding-github-actions

How can I use a Github action's output in a workflow?


Let's take this example composite action found on Github's documentation:

name: 'Hello World'
description: 'Greet someone'
inputs:
  who-to-greet:  # id of input
    description: 'Who to greet'
    required: true
    default: 'World'
outputs:
  random-number:
    description: "Random number"
    value: ${{ steps.random-number-generator.outputs.random-id }}
runs:
  using: "composite"
  steps:
    - run: echo Hello ${{ inputs.who-to-greet }}.
      shell: bash
    - id: random-number-generator
      run: echo "::set-output name=random-id::$(echo $RANDOM)"
      shell: bash
    - run: ${{ github.action_path }}/goodbye.sh
      shell: bash

How can we use that specific output random-number in an external workflow that calls this action? I tried the following snippet but currently it seems the workflow cannot read the output variable from the action as it just comes out empty - 'Output - '


jobs:
  test-job:
    runs-on: self-hosted
    steps:
      - name: Call Hello World 
        id: hello-world
        uses: actions/hello-world-action@v1
      - name: Comment
        if: ${{ github.event_name == 'pull_request' }}
        uses: actions/github-script@v3
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            github.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: 'Output - ${{ steps.hello-world.outputs.random-number.value }}'
            })

Solution

  • It seems my attempt was correct with the exception of one detail:

    Instead of:

    ${{ steps.hello-world.outputs.random-number.value }}
    

    It should be referenced without the .value:

    ${{ steps.hello-world.outputs.random-number}}
    

    Now it works.