DEV Community

Riku Rouvila
Riku Rouvila

Posted on • Edited on

How to trigger a Github action with an HTTP request

1. Create a new action with repository_dispatch trigger

Make sure your action is set to trigger on repository_dispatch event. This is the same event used when triggering the action through the UI.

name: Node.js CI

on:
  repository_dispatch:
  schedule:
    - cron: '5 12 * * 0'

jobs:
  build:
    runs-on: ubuntu-latest
Enter fullscreen mode Exit fullscreen mode

2. Get yourself a personal access token to use the Github API

You can create one here: https://github.com/settings/tokens

Make sure you add repo and workflow permissions

3. Create the HTTP request

https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#create-a-workflow-dispatch-event

POST https://api.github.com/repos/<username>/<repo>/dispatches
Authorization: Bearer <Your personal access token here>

{"event_type": "hello"}
Enter fullscreen mode Exit fullscreen mode

cURL

curl --request POST \
  --url 'https://api.github.com/repos/<USERNAME>/<REPO>/dispatches' \
  --header 'authorization: Bearer <TOKEN>' \
  --data '{"event_type": "hello"}'
Enter fullscreen mode Exit fullscreen mode

Sources

Top comments (3)

Collapse
 
yaroslavnakonechnikov profile image
yaroslav-nakonechnikov

is it possible to add some simple predefined link, which user can click and exec build in one go?

curl is fine, but would be awesome to add such link in release note, and let users to deploy with predefined settings releases.

Collapse
 
loq24 profile image
Lougie

Update in 2023:

You have to use token instead of Bearer. So it's going to be
Authorization: token <Your personal access token here>

Collapse
 
8ctopotamus profile image
Josh

Great example, exactly what I was looking for. Thanks for sharing!