簡體   English   中英

如何構建 yaml 文件來運行我的 python 代碼

[英]how to build a yaml file to run my python code

大家好,很抱歉這個愚蠢的問題,但這是我與 Yaml 的第二天。

  • 問題陳述:我有一個運行 12 分鍾的 python 代碼(所以我不能使用 Cloud Function 來自動化它),因此使用雲構建作為黑客。
  • 到目前為止完成的步驟:我在谷歌雲存儲庫中有我的代碼,我使用雲構建來構建圖像並創建了谷歌雲構建觸發器。 現在我想在每次觸發構建時運行 main.py python 代碼(我將使用 Cloud Scheduler 執行操作,如此處所述
  • 文件夾結構(如下圖所示)

雲存儲庫中的文件夾結構

  • 到目前為止我設法編寫的 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”的步驟?

感謝你的幫助。

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())'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM