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

2

22

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 }}'
            })
Adim answered 27/4, 2021 at 13:26 Comment(1)
Matrix output variables github.com/orgs/community/discussions/25634Harty
A
36

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.

Adim answered 27/4, 2021 at 13:42 Comment(2)
looks like set-output is now deprecated.......and ${{ steps.random-number-generator.outputs.random-id }} no longer seems to work :(Koski
set-outputs is being deprecated in favor of $GITHUB_OUTPUT, for some security concerns. See the GitHub blog for the official word. I found this action repo with a nice example to dealing with this change, with a function at line 53 and calls at lines 123-124.Bibliomania
K
0
jobs:
  job1:
    runs-on: ubuntu-latest
    # Map a step output to a job output
    outputs:
      output1: ${{ steps.step1.outputs.test }}
      output2: ${{ steps.step2.outputs.test }}
    steps:
    - id: step1
      run: echo "test=hello" >> "$GITHUB_OUTPUT"
    - id: step2
      run: echo "test=world" >> "$GITHUB_OUTPUT"
  job2:
    runs-on: ubuntu-latest
    needs: job1
    steps:
      - env:
        OUTPUT1: ${{needs.job1.outputs.output1}}
        OUTPUT2: ${{needs.job1.outputs.output2}}
      run: echo "$OUTPUT1 $OUTPUT2"
Katy answered 3/4 at 23:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.