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?
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 .
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.
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}/.
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.
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, seejobs.<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 apytest
or similar) and use that output in another job?
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.
actions/upload-artifact
and actions/download-artifact
. A bit awkward, but it works.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 }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With