Search code examples
github-actions

Use outputs in same job in github action returning empty value


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}}"

Solution

  • 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
    ...