Skip to content

Commit b3a6c52

Browse files
committed
Add Trigger A Workflow Via An API Call as a GitHub Actions TIL
1 parent dcdfae4 commit b3a6c52

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1315 TILs and counting..._
13+
_1316 TILs and counting..._
1414

1515
---
1616

@@ -347,6 +347,7 @@ _1315 TILs and counting..._
347347
- [Cache Playwright Dependencies Across Workflows](github-actions/cache-playwright-dependencies-across-workflows.md)
348348
- [Capture An Output Value For Use In A Later Step](github-actions/capture-an-output-value-for-use-in-a-later-step.md)
349349
- [Reference An Encrypted Secret In An Action](github-actions/reference-an-encrypted-secret-in-an-action.md)
350+
- [Trigger A Workflow Via An API Call](github-actions/trigger-a-workflow-via-an-api-call.md)
350351

351352
### Go
352353

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Trigger A Workflow Via An API Call
2+
3+
We can set up a GitHub Actions workflow to run when triggered by an API call.
4+
This is done with the [`workflow_dispatch`
5+
event](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_dispatch).
6+
7+
First, we add `workflow_dispatch` to our workflow as a triggering event:
8+
9+
```yaml
10+
on:
11+
workflow_dispatch:
12+
```
13+
14+
Second, we create a fine-grained personal GitHub access token that has permissions
15+
for dispatching to GitHub Actions. More details on that in the [GitHub
16+
docs](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token).
17+
18+
Then, we can use `cURL` or some other tool for issuing an HTTP POST request to
19+
[the workflow dispatch API
20+
endpoint](https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#create-a-workflow-dispatch-event). The `cURL` request will look something like this:
21+
22+
```bash
23+
curl -L \
24+
-X POST \
25+
-H "Accept: application/vnd.github+json" \
26+
-H "Authorization: Bearer <YOUR-TOKEN>"\
27+
-H "X-GitHub-Api-Version: 2022-11-28" \
28+
https://api.github.com/repos/OWNER/REPO/actions/workflows/WORKFLOW_ID/dispatches \
29+
-d '{"ref":"topic-branch","inputs":{"name":"Mona the Octocat","home":"San Francisco, CA"}}'
30+
```
31+
32+
Note: we need to alter that URL with the `OWNER` and `REPO` that the workflow
33+
lives in as well as the `WORKFLOW_ID` which can be the name of the workflow
34+
file (e.g. `my-dispatchable-workflow.yml`).
35+
36+
This event also means that we can manually trigger the workflow from the
37+
GitHub Actions UI for that workflow.

0 commit comments

Comments
 (0)