Sub topic 10
Running manually with workflow_dispatch
This chapter assumes you can read workflow YAML, know the on key forms from earlier chapters, and have used GitHub branches and Actions. Here you add a manual trigger to a workflow.
What workflow_dispatch does
workflow_dispatch lets a person or a script start a workflow on demand. Typical uses are deployments, one-off maintenance jobs, and re-running a check without pushing a commit.
Add it to the on key. With no options, it takes an empty value:
name: Manual job
on:
workflow_dispatch:
jobs:
hello:
runs-on: ubuntu-latest
steps:
- run: echo "Started by ${{ github.actor }} on ${{ github.ref_name }}"Once this file is on the default branch, GitHub shows a Run workflow button on the workflow's page in the Actions tab. You choose a branch or tag from a dropdown, and the run uses the workflow file from that ref.
Adding inputs
You can ask the person starting the run for values. Declare them under inputs. Each input has a description, and optionally a type, a default and a required flag.
on:
workflow_dispatch:
inputs:
environment:
description: Target environment
type: choice
options: [staging, production]
default: staging
dry_run:
description: Skip the real deployment
type: boolean
default: true
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Show chosen options
env:
TARGET: ${{ inputs.environment }}
DRY_RUN: ${{ inputs.dry_run }}
run: echo "Deploying to $TARGET (dry run: $DRY_RUN)"The supported types are string, boolean, choice and environment. Read values with the inputs context, as shown above. The older github.event.inputs also works, but it turns every value into a string.
Pass inputs to the shell through env rather than writing ${{ inputs.name }} directly inside the run script. GitHub substitutes expressions before the shell runs, so a free-text string input placed in a script can inject commands. An environment variable is treated as plain data.
Starting a run
There are three ways to start a manual run:
| Method | How |
|---|---|
| Web UI | Actions tab, pick the workflow, **Run workflow**, choose the ref and fill in inputs |
| GitHub CLI | `gh workflow run` |
| REST API | `POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches` |
With the GitHub CLI, --ref picks the branch and each -f sets an input:
gh workflow run deploy.yml --ref main -f environment=staging -f dry_run=falseThe REST API call needs a ref and an optional inputs object. It requires a token with permission to write to Actions.
How a run flows
Common mistakes
- Expecting the button to appear before the file is merged to the default branch.
- Using
inputswith more than the allowed number of properties. GitHub limits a workflow to 25 top-level inputs. - Forgetting that boolean inputs are real booleans in
inputsbut strings ingithub.event.inputs, soif: github.event.inputs.dry_run == 'true'andif: inputs.dry_rundiffer.
Exercise
You added workflow_dispatch to a workflow on a feature branch, but the Run workflow button does not appear in the Actions tab. What is the likely cause, and how do you fix it?
Show answer
The workflow file is not on the default branch, and the button only appears for workflows present there. Merge the file into the default branch. After that you can run it against the feature branch by choosing that ref in the dropdown or passing --ref to gh workflow run.
Exercise
Why should a string input be passed through env instead of being written as ${{ inputs.name }} inside a run script?
Show answer
GitHub replaces the expression with the input's text before the shell parses the script, so a crafted value can add shell commands. An environment variable is read by the shell as data, which prevents that injection.