如何使用 gcloud 命令行部署多个功能?



我想部署多个云函数。这是我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 functionsn"
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:

https://serverless.com/

如果有人正在寻找更好/更清洁/并行的解决方案,这就是我所做的:

# 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个。

这非常简单,节省了大量时间。希望它能帮助某人!

最新更新