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

Lets look at how to deploy a GitHub webhook trigger using wskdeploy.

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

To define an alarm trigger, you have to specify the source of the trigger as /whisk.system/github/webhook in the 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 the 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: 
                        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:

To verify your deployment commit some code to the GitHub repository, which in turn will return a simple greeting message. Ideally, you want to consume a commit payload that GitHub repository sends through HTTP POST. This payload can be accessed with params in an action:


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

Here is a snippet of commit payload that 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
    }
  }
],

Find more samples of webhook triggers on GitHub.

Enjoy!

Join The Discussion

Your email address will not be published. Required fields are marked *