17

GitHub Actions allows to trigger a workflow from another workflow using the workflow_run event as per this documentation: Events that trigger workflows - Webhook Events - workflow_run

This works fine. However, for the situations I am planning to use this all workflows except for the first one would likely rely on some information about the previous workflow... E.g. certain strings / conditionals / etc...

How can we pass data from one workflow to another?

Is there some reference similar to the needs.jobs.<job_id>.* which can be used to pass data from one job to another?

NOTE: Using an artifact built in workflow A from within workflow B is a different question (asked and answered here), which can be solved by using the following action: dawidd6/action-download-artifact@v2

fgysin
  • 11,329
  • 13
  • 61
  • 94
  • Did you try using a repository_dispatch trigger? With it, you can send a dispatch event from a workflow to start another one, sending some datas through the request payload. – GuiFalourd Apr 15 '21 at 11:37
  • 2
    Using `repository_dispatch` seems to be targeted at other situations. Since there is the `workflow_run` event specifically for my purpose I'd rather use that instead of building in a wacky workaround... – fgysin Apr 15 '21 at 11:42
  • Did you ever find a solution for this? I'm also in a similar sitation where repository_dispatcher doesn't quite seem to be the correct method since I'm not for a "trigger" option but rather use the output (data) from another workflow. – Fashinated Oct 25 '22 at 17:17
  • 1
    @Fashinated, It has been a while and I think eventually we solved this some other way... sorry for not having a good answer, IIRC the answer below was still the best option at the time. – fgysin Oct 26 '22 at 07:10

1 Answers1

12

You can use repository_dispatch action to send an event that contains the data you need. Then it will trigger another workflow that has on: repository_dispatch and a specific event name. Check the documentation of the action for more information.

You can pass the data you want inside client-payload. For bigger files I suppose artifacts can be used.

For example, you have your first workflow:


name: Test

on:
  - push
jobs:
  preflight-job:
    name: First Step
    runs-on: ubuntu-latest
    steps:
      - name: Repository Dispatch
        uses: peter-evans/repository-dispatch@v1
        with:
          token: ${{ secrets.PAT }}
          event-type: my-event
          client-payload: '{"ref": "${{ github.ref }}", "sha": "${{ github.sha }}", "test": "test message"}'

And then create another workflow file that will be triggered by this event:


name: Repository Dispatch
on:
  repository_dispatch:
    types: [my-event]
jobs:
  myEvent:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.event.client_payload.ref }}
          
      - run: echo ${{ github.event.client_payload.sha }}
      - run: echo ${{ github.event.client_payload.test }}
Denis Duev
  • 513
  • 3
  • 5