/dispatch-action

Github Action for triggering other workflows via message passing

Primary LanguageJavaScript

dispatch-action

Version License: MIT

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: arundo/dispatch-action@main
        with:
          # You need a GitHub token with repo read and write rights
          token: ${{ secrets.<name of token secret> }}
          repo: other-repo
          owner: arundo
          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: arundo/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

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

This repo has been forked from the personal GitHub of Mehdi Vasigh, former Arundo employee so that it can be maintained by Arundo 👤 Mehdi Vasigh mehdi.vasigh@gmail.com

Show your support

Give a ⭐️ if this project helped you!


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