
Artikel ini melanjutkan serangkaian catatan yang diterjemahkan tentang OpenWhisk oleh Priti Desai . Hari ini kita akan melihat contoh-contoh penyebaran webhook GitHub, sinyal periodik, serta aplikasi paling sederhana untuk mengirim pesan ke Slack.
Github webhook
Fungsi OpenWhisk dapat dipicu oleh berbagai peristiwa (misalnya, push, pull request, dll.) Dari repositori GitHub .
Mari kita lihat bagaimana Anda bisa menggunakan fungsi untuk menangani webhook GitHub menggunakan wskdeploy .
Langkah pertama
Buat file manifes:
Untuk menangani acara dengan GitHub, Anda perlu menentukan sumber acara /whisk.system/github/webhook
sebagai syarat untuk memicu:
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
Langkah kedua
Kami membuat file untuk penyebaran, di mana kami menetapkan nilai-nilai parameter input username
, repository
, accessToken
dan events
untuk pemrosesan bersyarat dari sumber acara 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
Langkah ketiga
Perluas fungsi:
./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.
Kami menyebarkan fungsi hello world , dipanggil dari GitHub setiap kali kami memperbarui kode dalam repositori GitHub, dan menggunakan webhook untuk ini:

Kami dapat memeriksa fungsi yang digunakan dengan membuat perubahan kode baru di repositori GitHub. Dalam hal ini, pesan selamat datang yang sederhana akan dikembalikan. Mari kita coba menggunakan payload ketika memperbarui kode yang dikirim dari GitHub melalui permintaan POST. Data ini tersedia sebagai parameter fungsi, misalnya:
function main(params) { console.log("GitHub repository is at ", params.repository.url); return {commits: params.commits}; }
Berikut ini cuplikan dari payload yang diperoleh dengan pengeditan normal 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 } } ],
Anda dapat mempelajari deskripsi terperinci tentang bagaimana webhook GitHub terbakar di sini.
Sinyal
Fungsi OpenWhisk secara berkala dapat dipicu oleh sinyal internal (seperti tugas cron). Mari kita coba menambahkan pemicu bersyarat pada sinyal melalui wskdeploy
.
Langkah pertama
Untuk memproses sinyal, Anda perlu menentukan sumber peristiwa / /whisk.system/alarms/alarm
sebagai kondisi untuk memicu dalam manifes:
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: Every12Hours: feed: /whisk.system/alarms/alarm rules: hellowroldOnCron: action: helloworld trigger: Every12Hours
Langkah kedua
Buat file untuk penyebaran, tentukan cron sebagai kondisi input, dengan nilai Every12Hours . Anda juga dapat menggunakan kedua opsi yang digunakan dalam cron, tradisional dan yang disempurnakan:
<Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week> <Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week> <Year>
Isi file deployment.yaml:
application: name: SampleHelloWorld namespace: _ packages: helloworld: actions: helloworld: inputs: name: Amy place: Paris triggers: Every12Hours: inputs: cron: "0 */12 * * *"
Dimungkinkan untuk menentukan payload yang dikirim ketika sinyal dipicu (dikirim sebagai parameter fungsi setiap kali dipicu) sebagai parameter trigger_payload :
application: name: SampleHelloWorld namespace: _ packages: helloworld: actions: helloworld: inputs: name: Amy place: Paris triggers: Every12Hours: inputs: cron: "0 */12 * * * *" trigger_payload: "{\"name\":\"Mark\", \"place\":\"Barcelona\"}"
Langkah ketiga
Perluas fungsi:
./wskdeploy -p ~/SampleHelloWorldApp/ ____ ___ _ _ _ _ _ /\ \ / _ \ _ __ ___ _ __ | | | | |__ (_)___| | __ /\ /__\ \ | | | | '_ \ / _ \ '_ \| | | | '_ \| / __| |/ / / \____ \ / | |_| | |_) | __/ | | | |/\| | | | | \__ \ < \ \ / \/ \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\ \___\/ |_| Packages: Name: helloworld bindings: * action: helloworld bindings: - name: name value: Amy - name: place value: Paris Triggers: * trigger: Every12Hours bindings: - name: cron value: 0 */12 * * * * annotations: - name: feed value: /whisk.system/alarms/alarm Rules * rule: helloworldEvery12Hours - trigger: Every12Hours - action: helloworld Do you really want to deploy this? (y/N): y Deploying package helloworld ... Done! Deploying action helloworld/helloworld ... Done! Deploying trigger feed Every12Hours ... Done! Deploying rule helloworldEvery12Hours ... Done! Deployment completed successfully.
Mulai sekarang, ac memiliki fungsi hello world yang berjalan setiap 12 jam. Kami dapat memeriksanya dengan mengaktifkan kondisi, seperti yang ditunjukkan sebelumnya
Paket untuk Slack
Untuk mengirim pesan ke Slack , sebuah paket untuk Slack ditawarkan. Mari kita lihat pengikatan paket menggunakan wskdeploy
.
Langkah pertama
Buat file manifes dengan paket yang mengikat untuk Slack. Untuk melakukan ini, tentukan sebagai ketergantungan, setel nilai lokasi ke /whisk.system/slack
:
packages: SlackPackage: dependencies: slack-package-to-post-messages: location: /whisk.system/slack inputs: username: $SLACK_USERNAME url: $SLACK_URL channel: $SLACK_CHANNEL
Pertama-tama, kita perlu mengkonfigurasi dukungan untuk webhook yang masuk di lingkungan kerja Slack kita. Anda dapat mengonfigurasi webhook baru untuk mengirim pesan ke saluran Slack menggunakan instruksi langkah-demi-langkah ini .
Sekarang kami menambahkan paket untuk Slack ke aplikasi kami untuk mengirim pesan ke saluran Slack setiap jam menggunakan manifes ini:
packages: SlackPackage: dependencies: slack-package-to-post-messages: location: /whisk.system/slack inputs: username: $SLACK_USERNAME url: $SLACK_URL channel: $SLACK_CHANNEL actions: post-to-slack: function: actions/post-to-slack.js runtime: nodejs:6 inputs: message: type: string description: message to post on slack slack_package: type: string description: slack package name triggers: everyhour: feed: /whisk.system/alarms/alarm rules: post-to-slack-every-hour: action: post-to-slack trigger: everyhour
Isi fungsi post-to-slack.js dapat dilihat di sini .
Langkah kedua
Buat file untuk penyebaran:
application: name: AppToPostToSlack packages: SlackPackage: actions: post-to-slack: inputs: message: "Hello from WskDeploy!" slack_package: slack-package-to-post-messages triggers: everyhour: inputs: cron: "0 */1 * * *"
Langkah ketiga
Perluas fungsi:
./wskdeploy -p ~/AppToPostToSlack/ ____ ___ _ _ _ _ _ /\ \ / _ \ _ __ ___ _ __ | | | | |__ (_)___| | __ /\ /__\ \ | | | | '_ \ / _ \ '_ \| | | | '_ \| / __| |/ / / \____ \ / | |_| | |_) | __/ | | | |/\| | | | | \__ \ < \ \ / \/ \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\ \ __\/ |_| Packages: Name: SlackPackage bindings: * dependency: slack-package-to-post-messages location: /whisk.system/slack * action: post-to-slack bindings: - message : "Hello from WskDeploy!" - slack_package : "slack-package-to-post-messages" annotations: Triggers: * trigger: everyhour bindings: - cron : "0 */1 * * *" annotations: - name: feed value: /whisk.system/alarms/alarm Rules * rule: post-to-slack-every-hour - trigger: everyhour - action: SlackPackage/post-to-slack Do you really want to deploy this? (y/N): y Deployment completed successfully.
Pada langkah ini, kami sudah memiliki fungsi post-to-slack yang berjalan 1 kali per jam. Anda dapat memverifikasinya dengan mengaktifkan kondisi, seperti yang ditunjukkan sebelumnya . Setelah aktivasi, kami akan menerima pesan baru di saluran Slack:
Activation: post-to-slack (9909dd5229e84526bff9902a2cd860df) [ "2017-09-12T23:05:17.17872899Z stdout: Hello from WskDeploy!", "2017-09-12T23:05:17.549177677Z stdout: Posted message to slack" ]
Artikel siklus lainnya
Komputasi tanpa server berdasarkan OpenWhisk, bagian 1
Komputasi tanpa server berdasarkan OpenWhisk, bagian 2
Komputasi tanpa server berdasarkan OpenWhisk, bagian 3
Komputasi tanpa server berdasarkan OpenWhisk, bagian 4