20

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 }}'
            })
Maslor
  • 1,821
  • 3
  • 20
  • 44

1 Answers1

33

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.

Maslor
  • 1,821
  • 3
  • 20
  • 44
  • 1
    looks like set-output is now deprecated.......and ${{ steps.random-number-generator.outputs.random-id }} no longer seems to work :( – Fuseteam Nov 02 '22 at 13:24
  • 2
    `set-outputs` is being deprecated in favor of `$GITHUB_OUTPUT`, for some security concerns. See the [GitHub blog](https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/) for the official word. I found [this action repo](https://github.com/anothrNick/github-tag-action/blob/master/entrypoint.sh) with a nice example to dealing with this change, with a function at line 53 and calls at lines 123-124. – Tom Willis Jan 23 '23 at 02:02