Google Build cloudbuild.yaml 部署 python 函数最佳实践

use*_*562 2 python-3.x google-cloud-platform google-cloud-functions google-cloud-build

我的项目结构设置如下:

cloudbuild.yaml
requirements.txt
functions/
    folder_a/
        test/
            main_test.py
        main.py
Run Code Online (Sandbox Code Playgroud)

我需要在 my 中指定什么才能cloudbuild.yaml获取 中每个新编辑的函数functions/,运行测试,然后将这些函数同步到 Google Cloud Functions?所有函数都是 python37 并使用 http 作为触发器。

小智 5

为什么不在每次代码更改时重新运行测试并重新部署?测试更新的依赖项没有破坏旧的东西或者你所依赖的东西可能已经改变并没有什么坏处。

我不确定你正在使用什么测试框架,但类似

steps:
- name: 'python'
  args: ['pip3','install', '-r', 'requirements.txt', '--user']
# This installs your requirements and `--user` makes them persist between steps

- name: 'python'
  args: ['python3','pytest', 'functions/folder_a/test/'] #run all tests in the tests folder

# Create a task for each function as shown here: https://cloud.google.com/functions/docs/bestpractices/testing#continuous_testing_and_deployment
- name: 'gcr.io/cloud-builders/gcloud']
    id: 'deployMyFunction'
  args: ['functions', 'deploy', 'my-function', '--source' , 'functions/folder_a/main.py', '--runtime' , 'python37' ,'--trigger-http']

# Option B: Write some python that iterates and deploys each function, although I can't seem to find the Cloud Functions in the python SDK SPI.
- name: 'python'
  args: ['python3','deploy.py']
  env:
  - 'PROJECT_ID=${_PROJECT_ID}'

Run Code Online (Sandbox Code Playgroud)