Search code examples
perlgithub-actions

Using output from a previous job in a new one in a GitHub Action


For (mainly) pedagogical reasons, I'm trying to run this workflow in GitHub actions:

name: "We 🎔 Perl"
on:
  issues:
    types: [opened, edited, milestoned]

jobs:
  seasonal_greetings:
    runs-on: windows-latest
    steps:
      - name: Maybe greet
        id: maybe-greet
        env:
          HEY: "Hey you!"
          GREETING: "Merry Xmas to you too!"
          BODY: ${{ github.event.issue.body }}
        run: |
          $output=(perl -e 'print ($ENV{BODY} =~ /Merry/)?$ENV{GREETING}:$ENV{HEY};')
          Write-Output "::set-output name=GREET::$output"
  produce_comment:
    name: Respond to issue
    runs-on: ubuntu-latest
    steps:
      - name: Dump job context
        env:
          JOB_CONTEXT: ${{ jobs.maybe-greet.steps.id }}
        run: echo "$JOB_CONTEXT"

I need two different jobs, since they use different context (operating systems), but I need to get the output of a step in the first job to the second job. I am trying with several combinations of the jobs context as found here but there does not seem to be any way to do that. Apparently, jobs is just the name of a YAML variable that does not really have a context, and the context job contains just the success or failure. Any idea?


Solution

  • Check the "GitHub Actions: New workflow features" from April 2020, which could help in your case (to reference step outputs from previous jobs)

    Job outputs

    You can specify a set of outputs that you want to pass to subsequent jobs and then access those values from your needs context.

    See documentation:

    jobs.<jobs_id>.outputs
    

    A map of outputs for a job.

    Job outputs are available to all downstream jobs that depend on this job.
    For more information on defining job dependencies, see jobs.<job_id>.needs.

    Job outputs are strings, and job outputs containing expressions are evaluated on the runner at the end of each job. Outputs containing secrets are redacted on the runner and not sent to GitHub Actions.

    To use job outputs in a dependent job, you can use the needs context.
    For more information, see "Context and expression syntax for GitHub Actions."

    To use job outputs in a dependent job, you can use the needs context.

    Example:

    jobs:
      job1:
        runs-on: ubuntu-latest
        # Expose step outputs as job outputs
        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:
        - run: echo ${{needs.job1.outputs.output1}} ${{needs.job1.outputs.output2}}
    

    Note the use of $GITHUB_OUTPUT, instead of the older ::set-output now (Oct. 2022) deprecated.

    To avoid untrusted logged data to use set-state and set-output workflow commands without the intention of the workflow author we have introduced a new set of environment files to manage state and output.


    Jesse Adelman adds in the comments:

    This seems to not work well for anything beyond a static string.
    How, for example, would I take a multiline text output of step (say, I'm running a pytest or similar) and use that output in another job?