Whisk Deploy — GitHub Webhook Trigger

Priti Desai
Apache OpenWhisk
Published in
3 min readMar 25, 2017

OpenWhisk actions can be invoked by a number of events (e.g. push, pull request, etc) from a GitHub repository.

Lets look at how we can deploy GitHub webhook trigger using wskdeploy:

Step 1: Create a manifest file (manifest.yaml)

To define a GitHub trigger, we have to specify source of the trigger as /whisk.system/github/webhook in manifest.yaml file:

packages:
helloworld:
actions:
helloworld:
location: src/hello.js
runtime: nodejs:6
inputs:
name:
type: string
description: name of a person
place:
type: string
description: location of a person
outputs:
payload:
type: string
description: a simple greeting message, Hello World!
triggers:
GitHubWebhookTrigger:
feed: /whisk.system/github/webhook
rules:
helloworldOnWebhook:
action: helloworld
trigger: GitHubWebhookTrigger

Step 2: Create a deployment file (deployment.yaml)

Set username, repository, accessToken, and events input parameters to GitHub trigger GitHubWebhookTrigger:

application:
name: SampleHelloWorld
namespace: _
packages:
helloworld:
actions:
helloworld:
inputs:
name: Amy
place: Paris
triggers:
GitHubWebhookTrigger:
inputs:
username: pritidesai
repository: pritidesai/helloworld
accessToken: <accessToken>
events: push

Step 3: Deploy a sample action

./wskdeploy -p ~/SampleHelloWorldApp/         ____      ___                   _    _ _     _     _
/\ \ / _ \ _ __ ___ _ __ | | | | |__ (_)___| | __
/\ /__\ \ | | | | '_ \ / _ \ '_ \| | | | '_ \| / __| |/ /
/ \____ \ / | |_| | |_) | __/ | | | |/\| | | | | \__ \ <
\ \ / \/ \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\
\___\/ |_|
Packages:
Name: helloworld
bindings:
* action: helloworld
bindings:
- name: name value: Amy
- name: place value: Paris
Triggers:
* trigger: GitHubWebhookTrigger
bindings:
- name: accessToken value: ****
- name: events value: push
- name: username value: pritidesai
- name: repository value: pritidesai/helloworld
annotations:
- name: feed value: /whisk.system/github/webhook
Rules
* rule: helloworldOnWebhook
- trigger: GitHubWebhookTrigger
- action: helloworld
Do you really want to deploy this? (y/N): y
Deploying package helloworld ... Done!
Deploying action helloworld/helloworld ... Done!
Deploying trigger feed GitHubWebhookTrigger ...
Done!
Deploying rule helloworldOnWebhook ... Done!
Deployment completed successfully.

We have deployed an action hello world running each time there is a new commit in GitHub repository with a new webhook:

We can verify our deployment by committing some code to the GitHub repository which in turn will return a simple greeting message. Ideally, we would like to consume commit payload which GitHub repository sends through HTTP POST. This payload can be accessed with params in an action, like:

function main(params) {
console.log("GitHub repository is at ", params.repository.url);
return {commits: params.commits};
}

Here is a snippet of commit payload which resulted from a simple commit to README.md:

"commits" : [
{
"author" : {
"name" : Priti Desai,
"username" : pritidesai
},
"timestamp" : 2017-03-20T12:54:41-07:00,
"removed" : [
],
"modified" : [
README.md
],
"added" : [
],
"message" : Update README.md,
"committer" : {
"name" : GitHub,
"email" : noreply@github.com,
"username" : web-flow
}
}
],

You can find more detailed sample of GitHub Webhook Trigger here.

Enjoy!

--

--

Priti Desai
Apache OpenWhisk

Developer Lead @IBM. Tekton maintainer. Co-founder of License Scanner @Cyclonedx