1

I have been seeing several examples (https://docs.github.com/en/actions/using-jobs/defining-outputs-for-jobs#example-defining-outputs-for-a-job) of calling outputs in different jobs, but I'm encountering this problem, of using the output in the same job.

This is an example I found in the githuvhttps://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#example-3

  - name: Set color
    id: random-color-generator
    run: echo "SELECTED_COLOR=green" >> $GITHUB_OUTPUT
  - name: Get color
    run: echo "The selected color is ${{ steps.random-color-generator.outputs.SELECTED_COLOR }}"

I'm under the impression that it has to show the result of 3. But just getting ""

Run echo ""
  echo ""
  shell: /usr/bin/bash -e {0}

Part of the code

jobs:
  build:
    outputs:
      fav-number: ${{steps.abc.outputs.FAV_NUMBER}}
    steps:
     - name: abc
        run: |
          echo '::set-output name=FAV_NUMBER::3' 
    - name: readoutout
        run: |
          echo "${{steps.abc.outputs.FAV_NUMBER}}"
0

1 Answer 1

5

To reference step outputs, it is necessary to assign unique IDs to these steps:

name: Custom outputs
on:
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    outputs:
      fav-number: ${{ steps.set-output.outputs.FAV_NUMBER }}
    steps:
      - name: Set Output
        id: set-output
        run: |
          echo '::set-output name=FAV_NUMBER::3'
      - name: Read output
        id: read-output
        run: |
          echo "${{ steps.set-output.outputs.FAV_NUMBER }}"

  test:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Read the build job output
        run: |
          echo "${{ needs.build.outputs.fav-number }}"

The example above demonstrates how to set and access the output of steps within a single job and how to access the output of the job from another job.

By the way, the set-output command is deprecated and will be disabled soon. So, I would suggest setting the output in the following way:

...
id: set-output
run: |
  echo "FAV_NUMBER=3" >> $GITHUB_OUTPUT
...
Sign up to request clarification or add additional context in comments.

2 Comments

Are you suggesting that even the step that is reading the outputs of another step needs an id? E.g. in your example, read-output shouldn't need an id, right?
Yes, the read-output is not necessary in this case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.