actions / runner

The Runner for GitHub Actions :rocket:
https://github.com/features/actions
MIT License
4.84k stars 952 forks source link

Cannot access `jobs.<job_id>.result` from `on.workflow_call.outputs.<output_id>.value` #3087

Open benjaminmal opened 9 months ago

benjaminmal commented 9 months ago

Describe the bug I'm trying to set an output in my reusable workflow of the result of a job but can't access to jobs.<job_id>.result from on.workflow_call.outputs.<output_id>.value. I have a empty string. As stated in docs, I should be able to access it. I can correctly access my output from my caller workflow.

To Reproduce Reusable workflow file:

on:
  workflow_call:
    outputs:
      result:
        value: ${{ jobs.build.result }}

jobs:
  build:
    if: 'true' == 'false' # Makes skip the job
    runs-on: ubuntu-latest
    steps:
      -  run: echo 'hello-world!'

Caller workflow file:

on:
  push: ~

jobs:
  build:
    uses: ./.github/workflows/build.yaml
    secrets: inherit

  deploy:
    needs: build
    uses: ./.github/workflows/deploy.yaml
    if: needs.build.outputs.result == 'skipped'
    secrets: inherit

Expected behavior jobs.<job_id>.result must be skipped, I've got an empty string

Runner Version and Platform

Version of your runner? 2.311.0 GitHub Hosted

OS of the machine running the runner? ubuntu-latest

What's not working?

Doing (reusable workflow):

on:
  workflow_call:
    outputs:
      result:
        value: ${{ toJSON(jobs.build) }}

And (caller workflow):

on:
  push: ~

jobs:
  debug:
    needs: build
    steps:
      -  run: echo ${{ needs.build.outputs.result }}

Print: Capture d’écran 2024-01-13 à 23 10 36

Workaround

I found an interesting workaround:

on:
  workflow_call:
    outputs:
      result:
        value: ${{ fromJSON(toJSON(jobs.build)).result }}

Works perfectly. I can access it in my caller workflow:

Capture d’écran 2024-01-13 à 23 15 09

rosiejeays commented 5 months ago

Hey!

I had a similar issue when I was trying to grab an output from a reusable workflow. My problem was that I was only defining the outputs in the job and not at the top level of the workflow. Your post helped me find what I was missing. You seem to maybe have the opposite happening - only the outputs defined at the top level and not in the job, however it's missing the 'outputs' call. It looks like the GitHub output is also not being set in the run command.

Hopefully trying this will help you:

on:
  workflow_call:
    outputs:
      result:
        value: ${{ jobs.build.outputs.result }} # set this to call on the result value in the outputs in the build job

jobs:
  build:
    runs-on: ubuntu-latest
    outputs: 
      result: ${{ steps.hello-world.outputs.result}} # Define the output here with using the step id
    steps:
      - id: hello-world # provide an id to the step you are wanting to call on
        run: |
          result='hello world!' 
          echo "result=$result" >> $GITHUB_OUTPUT # Set the variable as GitHub output