How to Trigger Another GitHub Actions Workflow with the Dispatch Action

Nov 29, 2023 | Programming

If you’re looking to streamline your CI/CD pipelines in GitHub, leveraging the workflow dispatch action can be a game changer. This feature allows you to trigger another GitHub Actions workflow using the workflow_dispatch event. In this article, we’ll dive into how to set up and configure this powerful action, step by step.

What is the Workflow Dispatch Action?

The workflow dispatch action is designed to facilitate the chaining of workflows. Imagine you have a continuous integration (CI) build workflow that, once completed, can trigger a continuous deployment (CD) release workflow. This functionality not only helps maintain organized workflows but also allows you to pass data between them.

Setting Up the Workflow Dispatch Action

To get started, you need to ensure the target workflow is configured for workflow_dispatch. You can do this by adding the following line to your workflow configuration file:

on: [workflow_dispatch]

Here’s how you can use the action in your GitHub repository:

Action Inputs

  • workflow: (Required) Specify the name, filename, or ID of the workflow you wish to trigger, such as My Workflow or my-workflow.yaml.
  • inputs: (Optional) Any JSON-encoded inputs to pass to the triggered workflow.
  • ref: (Optional) The Git reference for the triggered workflow run (branch, tag, or commit SHA).
  • repo: (Optional) To trigger workflows in another repository, provide the owner and repo name.
  • token: (Optional) The token needed when triggering workflows across repositories.

Example Usage

Now let’s look at practical examples of how to utilize this action:

name: Invoke workflow without inputs
uses: benc-uk/workflow-dispatch@v1
with:
  workflow: My Workflow
name: Invoke workflow with inputs
uses: benc-uk/workflow-dispatch@v1
with:
  workflow: Another Workflow
  inputs:
    message: blah blah
    something: true
name: Invoke workflow in another repo with inputs
uses: benc-uk/workflow-dispatch@v1
with:
  workflow: my-workflow.yaml
  repo: benc-uk/example
  inputs:
    message: blah blah
    something: false
  token: ${{ secrets.MY_TOKEN }}

Understanding with an Analogy

Think of GitHub Actions like an orchestra. Each workflow represents a different section of the orchestra — strings, brass, woodwinds, etc. The workflow dispatch action is akin to a conductor signaling the brass section to play after the strings have finished their piece. This sequential coordination allows for a harmonious performance, where each section contributes to a larger symphony: the complete deployment process. In this case, you get to create flows between CI and CD in a well-orchestrated manner!

Troubleshooting Tips

While using the workflow dispatch action, you might encounter a few hiccups. Here are some common troubleshooting steps:

  • If you see an error like “Resource not accessible by integration,” make sure you’re providing the correct token, especially when triggering workflows across repositories.
  • If you receive a “No ref found” error, ensure the target branch exists in the external repository and provide the relevant ref.
  • Refer to the official GitHub documentation for the most updated practices on reusing workflows.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

By using the workflow dispatch action effectively, you can create a more efficient CI/CD pipeline, allowing for better separation of concerns in your development workflows. At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox