在终端中将多个功能部署到Google Cloud Functions? [英] Deploy multiple functions to Google Cloud Functions in the terminal?
问题描述
要为单个触发事件部署单个功能,我们可以按照部署 Google云功能:
To deploy a single function for a single trigger event we can follow the instructions as outlined in the documentation on deploying Google Cloud Functions:
gcloud functions deploy NAME --runtime RUNTIME TRIGGER [FLAGS...]
平均部署时间为30s-2m,这是合理且合理的.
It takes on average 30s-2m to deploy, which is fine and reasonable.
但是,我想知道是否可以编写一个脚本(例如在python中)一次部署多个功能?
However, I was wondering if its possible to write a script (e.g. in python) to deploy multiple functions at once?
例如:
//somefile.py
gcloud functions deploy function_1 --runtime RUNTIME TRIGGER [FLAGS...]
gcloud functions deploy function_2 --runtime RUNTIME TRIGGER [FLAGS...]
推荐答案
我真的很喜欢使用调用此类问题的库.特别是,它非常适合在Python脚本中运行bash命令(例如gcloud
)而不会在subprocess
中造成麻烦.
I really like to use the invoke library for problems like this. In particular, it is well suited for running bash commands (e.g. gcloud
) within a Python script without mucking about in subprocess
.
根据您的情况,您可以制作一个外观如下的tasks.py
文件
In your case, you could make a tasks.py
file that looks like
from invoke import task
@task
def deploy_cloud_functions(c):
c.run('gcloud functions deploy function_1 --runtime RUNTIME TRIGGER [FLAGS...]')
c.run('gcloud functions deploy function_2 --runtime RUNTIME TRIGGER [FLAGS...]')
然后通过调用
invoke deploy-cloud-functions
请注意,如果您为函数
deploy_cloud_functions
命名,则必须使用:invoke deploy-cloud-functions
(请注意-)来调用它.您可以使用invoke --list
Note that if you name your function
deploy_cloud_functions
you have to call it using:invoke deploy-cloud-functions
(note the -). You can find a list of current available tasks in your directory usinginvoke --list
您也可以使用线程库对其进行并行化(尽管我尚未在调用自身中测试使用它).当然,它将在控制台中产生难看的输出.即
You can also parallelize it using the threading library (though I haven't tested using it within invoke myself). It will definitely make for ugly output in the console though. I.e.
from threading import Thread
from invoke import task
@task
def deploy_cloud_functions(c):
Thread(lambda x:
c.run('gcloud functions deploy function_1 --runtime RUNTIME TRIGGER [FLAGS...]')
).start()
Thread(lambda x:
c.run('gcloud functions deploy function_2 --runtime RUNTIME TRIGGER [FLAGS...]')
).start()
这篇关于在终端中将多个功能部署到Google Cloud Functions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!