Dynamic Outputs for Action

Hi! I’m creating a reusable action and the action will have a dynamic set of outputs based on the input. The output keys are not known until after the action has run (a json file is generated by the action with key/value mappings in it). I’m having a hard time figuring out if this is possible. In my workflow, I’ve tried to reference step output directly in the action, I’ve also tried defining a output named mapping then set it like this:

outputs:
  mapping:
    description: Mapping Output
    value: ${{ steps.my_step.outputs }}

but output values cannot be a mapping type. Has anyone got something like this working?

Edit: I should clarify that my action is using the ‘composite’ run type and if I declare an output in my action.yaml for a specific use case it works. I just won’t know all possible output keys for all use cases. But that confirmed it wasn’t an error in my workflow reading the output value.

Thanks!

1 Like

I think outputs can be only strings. So if you have an arbitrary dynamic JSON that you want to expose as an output, you can

  • write it to a file and expose its location as an output, then read that file in the subsequent steps
  • expose the JSON value as a string output, e.g. result, then you can convert it to JSON on the call site like this: ${{ fromJson(steps.my_step.outputs.result) }} (see docs about fromJson)

Thanks for the response, this seems to be a limitation of the ‘composite’ run type. The example for the javascript action shows the action not needing to declare outputs. I think this is because there are multiple steps for a composite run type and each of those may have the same output key. So GitHub does not automatically make all outputs available from the action, they must be explicitly defined and explicitly set from the step they came from. I’m in the process of migrating to a javascript action instead of composite.

Edit: Did confirm that when I migrated to a javascript action dynamic outputs just worked, I didn’t have to declare anything first. I just set the outputs in the action javascript and my workflow was able to read them all.

1 Like