Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 992
jjmerelo Avatar asked Dec 04 '19 11:12

jjmerelo


People also ask

How do I run one job after another action in GitHub?

To run jobs sequentially, you can define dependencies on other jobs using the jobs. <job_id>. needs keyword. Each job runs in a runner environment specified by runs-on .

How do I pass variables from one workflow to another in GitHub Actions?

If you want to pass a value from a step in one job in a workflow to a step in another job in the workflow, you can define the value as a job output. You can then reference this job output from a step in another job.

How do I trigger one workflow from another workflow in GitHub Actions?

You call a reusable workflow by using the uses keyword. Unlike when you are using actions within a workflow, you call reusable workflows directly within a job, and not from within job steps. You reference reusable workflow files using one of the following syntaxes: {owner}/{repo}/.

What are outputs in GitHub Actions?

Outputs are Unicode strings, and can be a maximum of 1 MB. The total of all outputs in a workflow run can be a maximum of 50 MB. 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.


2 Answers

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     # Map a step output to a job output     outputs:       output1: ${{ steps.step1.outputs.test }}       output2: ${{ steps.step2.outputs.test }}     steps:     - id: step1       run: echo "::set-output name=test::hello"     - id: step2       run: echo "::set-output name=test::world"   job2:     runs-on: ubuntu-latest     needs: job1     steps:     - run: echo ${{needs.job1.outputs.output1}} ${{needs.job1.outputs.output2}} 

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?

  • either write the multi-line text to a file (jschmitter's comment)
  • or base64-encode the output and then decode it in the next job (Nate Karasch's comment)
like image 73
VonC Avatar answered Sep 18 '22 16:09

VonC


Update: It's now possible to set job outputs that can be used to transfer string values to downstream jobs. See this answer.

What follows is the original answer. These techniques might still be useful for some use cases.

  1. Write the data to file and use actions/upload-artifact and actions/download-artifact. A bit awkward, but it works.
  2. Create a repository dispatch event and send the data to a second workflow. I prefer this method personally, but the downside is that it needs a repo scoped PAT.

Here is an example of how the second way could work. It uses repository-dispatch action.

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"       - name: Repository Dispatch         uses: peter-evans/repository-dispatch@v1         with:           token: ${{ secrets.REPO_ACCESS_TOKEN }}           event-type: my-event           client-payload: '{"greet": "${{ steps.maybe-greet.outputs.GREET }}"}' 

This triggers a repository dispatch workflow in the same repository.

name: Repository Dispatch on:   repository_dispatch:     types: [my-event] jobs:   myEvent:     runs-on: ubuntu-latest     steps:       - run: echo ${{ github.event.client_payload.greet }} 
like image 33
peterevans Avatar answered Sep 20 '22 16:09

peterevans