Workflow Dispatch in GitHub Actions
1. Introduction
Workflow dispatch in GitHub Actions allows you to manually trigger workflows from the GitHub interface. This feature can be particularly useful for workflows that need to be run under specific circumstances, such as deployments or testing scenarios.
2. Key Concepts
- Workflow: A configurable automated process that runs one or more jobs.
- Dispatch Event: A GitHub event that allows you to manually trigger a workflow.
- Inputs: Parameters that can be passed to a workflow when it is triggered manually.
3. Setting Up a Workflow Dispatch
To set up a workflow that can be manually triggered, you need to define it in your YAML file. Below is a step-by-step guide:
- Create a workflow file in the `.github/workflows` directory of your repository.
- Define the `on` trigger with `workflow_dispatch`:
- Optionally, define inputs for the workflow:
- Add jobs that you want to run when the workflow is triggered.
name: Manual Trigger Workflow
on:
workflow_dispatch:
inputs:
example_input:
description: 'An example input'
required: true
default: 'default value'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Run a script
run: echo "Input value is ${{ github.event.inputs.example_input }}"
4. Best Practices
- Use meaningful names for your workflows and inputs to improve clarity.
- Test your workflows regularly to ensure they function correctly when triggered.
- Limit the number of required inputs to reduce user error during manual triggering.
- Utilize default values for inputs when possible to simplify the triggering process.
5. FAQ
What is the purpose of workflow dispatch?
Workflow dispatch allows users to manually trigger workflows for scenarios where automated triggers aren't suitable, such as specific testing or deployment events.
Can I use inputs with workflow dispatch?
Yes, inputs can be defined in the workflow YAML file and passed when manually triggering the workflow.
How can I test my workflow dispatch?
You can test it by going to the "Actions" tab in your repository, selecting your workflow, and using the "Run workflow" button.