Skip to content

Github Action for triggering other workflows via message passing

License

Notifications You must be signed in to change notification settings

qoohoo-app/dispatch-action

 
 

Repository files navigation

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/dispatch-action@main
        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/checkout@v2
      - 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/dispatch-action@main
    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 with the repo scope enabled (if using with private repositories) or public_repo (for use only with public repositories)

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

About

Github Action for triggering other workflows via message passing

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 99.6%
  • Shell 0.4%