我想部署多个云功能。这是我的
index.js
:
const { batchMultipleMessage } = require('./gcf-1');
const { batchMultipleMessage2 } = require('./gcf-2');
module.exports = {
batchMultipleMessage,
batchMultipleMessage2
};
如何使用
gcloud beta functions deploy xxx
同时部署这两个功能。
选项1:
目前我写了一个
deploy.sh
来一次性部署这两个云功能。
TOPIC=batch-multiple-messages
FUNCTION_NAME_1=batchMultipleMessage
FUNCTION_NAME_2=batchMultipleMessage2
echo "start to deploy cloud functions\n"
gcloud beta functions deploy ${FUNCTION_NAME_1} --trigger-resource ${TOPIC} --trigger-event google.pubsub.topic.publish
gcloud beta functions deploy ${FUNCTION_NAME_2} --trigger-resource ${TOPIC} --trigger-event google.pubsub.topic.publish
它可以工作,但是如果
gcloud
命令行支持部署多个云功能,那将是最好的方法。
选项2:
如果有人正在寻找更好/更清洁/并行的解决方案,这就是我所做的:
# deploy.sh
# store deployment command into a string with character % where function name should be
deploy="gcloud functions deploy % --trigger-http"
# find all functions in index.js (looking at exports.<function_name>) using sed
# then pipe the function names to xargs
# then instruct that % should be replaced by each function name
# then open 20 processes where each one runs one deployment command
sed -n 's/exports\.\([a-zA-Z0-9\-_#]*\).*/\1/p' index.js | xargs -I % -P 20 sh -c "$deploy;"
您还可以更改通过
-P
标志传递的进程数。我随意选了20个。
这非常简单,节省了大量时间。希望它能帮助别人!
如果您不/不能使用脚本但更喜欢将其保留在 package.json 中,则还有另一种部署方式。
唯一的缺点是您需要重复
npm run deploy:function functionX
才能获得尽可能多的功能。
"scripts": {
"deploy:function": "gcloud functions deploy $1 --trigger-http",
"deploy:functions": "npm run deploy:function function1 && npm run deploy:function function2",
"deploy": "npm run deploy:functions",
},