はじめに
本記事は、下記を試したメモです。
cloudevents
cloudeventsをインストール
go install github.com/cloudevents/conformance/cmd/cloudevents@latest
※READMEには、go getと記載がある
パス追加
export PATH=$PATH:$(go env GOPATH)/bin
確認
cloudevents --help
インストール
npm install @google-cloud/functions-framework
index.js追加
index.js
const functions = require('@google-cloud/functions-framework');
functions.cloudEvent('helloCloudEvents', (cloudevent) => {
console.log(cloudevent.specversion);
console.log(cloudevent.type);
console.log(cloudevent.source);
console.log(cloudevent.subject);
console.log(cloudevent.id);
console.log(cloudevent.time);
console.log(cloudevent.datacontenttype);
});
package.jsonに下記追加
package.json
{
"scripts": {
"start": "functions-framework --target=helloCloudEvents"
}
}
実行
npm start
> start
> npx functions-framework --target=helloCloudEvents
Serving function...
Function: helloCloudEvents
Signature type: cloudevent
URL: http://localhost:8080/
cloudevents実行
cloudevents send http://localhost:8080 --id abc-123 --source cloudevents.conformance.tool --type foo.bar
実行結果
> start
> npx functions-framework --target=helloCloudEvents
Serving function...
Function: helloCloudEvents
Signature type: cloudevent
URL: http://localhost:8080/
1.0
foo.bar
cloudevents.conformance.tool
undefined
abc-123
undefined
undefined
所感
これを使えばGoogle Cloud Consoleで「保存してデプロイ」を何回もクリックすることは、回避できそう!
参考