【问题标题】:how to build a yaml file to run my python code如何构建一个 yaml 文件来运行我的 python 代码
【发布时间】:2020-08-27 02:42:55
【问题描述】:

大家好,很抱歉这个愚蠢的问题,但这是我使用 Yaml 的第 2 天。

  • 问题说明: 我有一个运行 12 分钟的 python 代码(所以我不能使用 Cloud Function 来自动化它),因此使用云构建作为 hack。
  • 到目前为止已完成的步骤: 我在谷歌云存储库中有我的代码,我使用云构建来构建图像并创建了谷歌云构建触发器。现在我想在每次触发构建时运行 main.py python 代码(我将使用 Cloud Scheduler 执行此操作,如 here 所述
  • 文件夹结构(如下图)

  • cloudbuild.yaml 到目前为止我设法编写的
steps:
- name: 'gcr.io/$PROJECT_ID/p2p-cloudbuild' 
  entrypoint: '/bin/bash'
  args: ['-c','virtualenv /workspace/venv' ]
  # Create a Python virtualenv stored in /workspace/venv that will persist across container runs.

- name: 'gcr.io/$PROJECT_ID/p2p-cloudbuild' 
  entrypoint: 'venv/bin/pip'
  args: ['install', '-V', '-r', 'requirements.txt']
  # Installs any dependencies listed in the project's requirements.txt.

问题:如何在 main.py 文件中添加调用/执行“my_function”的步骤?

感谢您的帮助。

【问题讨论】:

    标签: python docker google-cloud-platform yaml google-cloud-build


    【解决方案1】:
    steps:
    - name: 'gcr.io/$PROJECT_ID/p2p-cloudbuild' 
      entrypoint: '/bin/bash'
      args: ['-c','virtualenv /workspace/venv' ]
      # Create a Python virtualenv stored in /workspace/venv that will persist across container runs.
    
    - name: 'gcr.io/$PROJECT_ID/p2p-cloudbuild' 
      entrypoint: 'venv/bin/pip'
      args: ['install', '-V', '-r', 'requirements.txt']
      # Installs any dependencies listed in the project's requirements.txt.
    

    假设我有一个文件main.py

    def foo():
      return "bar"
    

    这实际上可以简化为:

    - name: 'gcr.io/$PROJECT_ID/p2p-cloudbuild' 
      entrypoint: '/bin/bash'
      args:
        - '-c'
        - |
          virtualenv /workspace/venv
          source /workspace/venv/bin/activate
          pip install -V -r requirements.txt
          python -c 'from main import foo; print (foo())'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2022-11-18
      • 2011-05-18
      • 2023-02-08
      • 2017-05-05
      • 1970-01-01
      相关资源
      最近更新 更多