Trigger a GitHub action with an HTTP Request

close up photo of programming of codes
Reading Time: 2 minutes

Hi folks in this blog we will see how we can Trigger a GitHub action with a HTTP Request using curl command.This is very quick and simple so let’s see how we can Trigger a GitHub action in few easy steps..

First lets see what is curl.

Curl

Curl stands for client URL. It is a command-line tool that we can easily use to transfer data to and from a server. It let’s us talk to a server by specifying We will use curl to send API requests.

For successfully triggering a github action we have to perform three main steps.These three steps are given below.

  • Create github action.
  • Generate a personal access token.
  • Create a http request.

Step first:

You can use the GitHub API to trigger a webhook event called repository_dispatch when you want to trigger a workflow for activity that happens outside of GitHub.We are triggering the github action outside the github so we must create with repository_dispatch event.

Now first create a new Github action with repository_dispatch event.This is the same event used when triggering the action through the UI.I have created a .yml file you can see here.

name: Do Something That Needs Scheduled

on:
  repository_dispatch:
    types: do-something
jobs:
  build:
    name: Run Some Thing
    runs-on: ubuntu-latest
    steps:
      - name: Do Something
        run: echo Doing Something...

Step Second:

For triggering a github action through curl command you need a personal access token to use the Github API.So make sure you have a personal access token if not you can create a new one by using following link.

You can create one here: https://github.com/settings/tokens.Make sure you add repo and workflow permissions.

Step Third:

To trigger the custom repository_dispatch webhook event you must send a POST request to a GitHub API endpoint.

And provide an event_type name to describe the activity type.

To trigger a workflow run ,also configured your workflow to use the repository_dispatch event as I have already defined.

curl -H "Accept: application/vnd.github.everest-preview+json" \
    -H "Authorization: token <your personal access token>" \
    --request POST \
    --data '{"event_type": "do-something"}' \
    https://api.github.com/repos/<username>/<repo>/dispatches

Perform above command on your terminal and if you want to check that the GitHub action is trigger or not you can check from following command.

curl -H "Accept: application/vnd.github.everest-preview+json" \
    -H "Authorization: token <your personal access token>" \
  --request GET   \
  --data '{"event_type": "do-something"}' \
  https://api.github.com/repos/<username>/<repo>/actions/runs

References:

https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#repository_dispatch

https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#repository_dispatch

Conclusion

In this blog we have seen how in few easy steps we can trigger a github action job remotely via curl.If you have any query tell me in comment section and also share this blog to your friends.Thank You.

knoldus footer

Written by 

Jubair Ahmad is a Software Intern at Knoldus.Inc.He loves learning new technology and also have interest in playing cricket.

Leave a Reply