Blog
Dispatch a Workflow from Another Workflow Step in GitHub Actions
, John Comber
Introduction
This example demonstrates how to dispatch a workflow from another workflow step in GitHub Actions.
Instructions
Create a fine grained personal access token <PAT TOKEN>
with Read and Write access to Actions and replace the placeholder in the script with this value. Also replace <OWNER>
, <REPOSITORY>
, <WORKFLOW NAME OR ID>
and <ANY INPUTS AS JSON>
with values for your repository.
Error Response Codes
It's possible to instruct CURL to fail on a non 2xx response and return a non-zero error code to the shell (which would fail the GitHub Action Step) using the -f
flag. However, the man page describes that this doesn't always work for authentication failure responses (e.g. 401, 407).
In this example, I'm returning the response code from CURL into an environment variable and then explicitly testing to check if the response wasn't 204 and, if so, return exit code 1 so that the GitHub Actions Step fails.
Code
- name: Dispatch Workflow
run: >-
RESPONSE_CODE=$(curl
-s
-o /dev/null
-w "%{http_code}"
-X POST
-H "Accept: application/vnd.github+json"
-H "Authorization: Bearer <PAT TOKEN>"
-H "X-GitHub-Api-Version: 2022-11-28"
https://api.github.com/repos/<OWNER>/<REPOSITORY>/actions/workflows/<WORKFLOW NAME OR ID>/dispatches
-d '{ "ref":"main", "inputs": { <ANY INPUTS AS JSON> } }')
if test $RESPONSE_CODE -ne 204; then
echo "Response: $RESPONSE_CODE"
exit 1
fi