Search code examples
github-actions

GitHub Actions: pass data from workflow to another workflow?


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


Solution

  • 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 }}