Webtask as a GitHub Web Hook

A Webtask can be the implementation of a GitHub web hook

For customized documentation and ready to run samples, please log in.

You can implement a Webtask that executes every time a GitHub web hook fires, without worrying about where to host your code. Not only can you run custom code on a web hook event using Webtasks, you can also store this code in the very GitHub repository which generates the web hook event.

Consider a webtask with a fixed implementation that receives the web hook payload from GitHub whenever a user pushes to the repository. The webtask code can turn around and obtain particular file from the repository that generated the web hook event, and then execute the code from that file. This allows you very easily implement a variety of devops and intergation scenarios triggered by GitHub web hooks, all while maintaining the logic within the repository itself.

Let's get started. Review the webtask code of our meta-webtask. We are going to issue a webtask token that executes this code when invoked:

echo https://webtask.it.auth0.com/api/run/{webtask_container}?key=$(curl -s https://webtask.it.auth0.com/api/tokens/issue -H "Authorization: Bearer {webtask_token}" -H "Content-Type: application/json" --data-binary '{"url":"https://raw.githubusercontent.com/auth0/webtask-scripts/master/github/smarthook.js"}')

Next, install the generated URL as a GitHub web hook in the public repository of your choice. Select application/json as the content type of the web hook payload.

Lastly, add a webtask.js file to the root of your repository. This is the file that the webtask code will obtain and execute when the web hook fires. The programming model of the webtask.js is similar to that of a webtask itself:

module.exports = function(context, callback) {
  // context.webhook contains the webhook payload provided by GitHub
  // context.data contains URL query and webtask token parameters
  callback(null, { some: 'result' });

The function will execute in the same environment as the webtask code itself, and is subject to the same constraints. This means this mechanism can be successfuly used to execute lighweight integration logic, but is probably not a good choice for running your entire CI process from.

Consider this example which sends a message to a Slack channel when a change in a repository is reported. Take note of the slack_token and slack_channel parameters that are required. These parameters can be provided either through the URL query parameters of the GitHub web hook, or by securely embedding them in the webtask token itself at the time of issuance.