Awesome Open Source
Awesome Open Source

dispatch-action

Version License: MIT Twitter: mehdi_vasigh

Create repository dispatch events to trigger a Github workflow from within another workflow

🚀 Usage

To use this action, you need to create a Github workflow file in your repository under the .github/workflows directory. You also must create a Github personal access token, which should be stored as a secret in your Github repository.

Here is an example of a Github workflow file that uses this action to trigger a sample-push event in another repository:

name: Alert parent repository on push
on: push

jobs:
  build:
    name: Dispatch to `other-repo`
    runs-on: ubuntu-latest
    steps:
      - name: Emit repository_dispatch
        uses: mvasigh/[email protected]
        with:
          # You should create a personal access token and store it in your repository
          token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
          repo: other-repo
          owner: mvasigh
          event_type: sample_push

The emitted event can be consumed in another Github workflow, either within the same repository or another one. Here's an example:

name: Print on external push

# Controls when the action will run. Triggers the workflow on repository_dispatch and filters by type of event (i.e. `event_type`)
on:
  repository_dispatch:
    types: [sample_push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/[email protected]
      - name: Run a one-line script
        run: echo Hello, world!
      - name: Run a multi-line script
        run: |
          echo Add other custom actions to build,
          echo test, and deploy your project.

By default, this action will hydrate the client_payload of the repository dispatch event with the original event payload under the event key (equivalent to github.context.payload). You can also send any additional data by supplying the message input option, which will also be available under the message key of the client_payload object. This can be used in other workflows by accessing ${{ github.event.client_payload.message }} in any workflow triggered by this action.

Here is an example:

# Dispatcher workflow in submodule
steps:
  - name: Dispatch submodule_push event
    uses: mvasigh/[email protected]
    with:
      token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
      repo: '@'
      event_type: submodule_push
      message: |
        {
          "foo": "bar"
        }
# Consuming workflow in parent repository
steps:
  - name: Print custom message
    run: echo ${{ github.event.client_payload.message.foo }} # bar

📝 Options

This action accepts the following options:

token (required) - a Github personal access token

event_type (required) - a name for the type of event that you are emitting

repo - name of the repository to dispatch event to (same repository by default)

owner - Github org/name of the repository's owner (event sender's name by default)

message - optional data to send along with the event (must be a JSON string)

Author

👤 Mehdi Vasigh [email protected]

Show your support

Give a ⭐️ if this project helped you!


This README was generated with ❤️ by readme-md-generator



Alternative Project Comparisons
Related Awesome Lists
Top Programming Languages
Top Projects

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Javascript (1,074,350
Actions (55,475
Github (43,910
Message Passing (264