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

时间:2018-10-10 04:32:41

标签: google-cloud-functions gcloud

我想部署多个云功能。这是我的index.js

const { batchMultipleMessage } = require('./gcf-1');
const { batchMultipleMessage2 } = require('./gcf-2');

module.exports = {
  batchMultipleMessage,
  batchMultipleMessage2
};

如何使用gcloud beta functions deploy xxx一次部署这两个功能。

2 个答案:

答案 0 :(得分:1)

选项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:

https://serverless.com/

答案 1 :(得分:0)

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

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

这非常容易,并节省了大量时间。希望它将对某人有所帮助!