【问题标题】:How to deploy multiple functions using gcloud command line?如何使用 gcloud 命令行部署多个功能?
【发布时间】:2023-12-06 05:13:01
【问题描述】:

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

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

module.exports = {
  batchMultipleMessage,
  batchMultipleMessage2
};

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

【问题讨论】:

  • 您是否尝试过只使用没有任何参数的部署,例如使用 Firebase。在 Firebase 中,如果您使用 firebase deploy 它会部署它在 index.js 中找到的每个函数。

标签: google-cloud-functions gcloud


【解决方案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/

【讨论】:

    【解决方案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个。

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

    【讨论】:

    • 感谢脚本,它运行良好!但是,我从生成的命令行中删除了--trigger-http,因为你可能有其他类型的函数(我有firestore触发器,所以它是无效的)。
    最近更新 更多