Search code examples
pythondockergoogle-cloud-platformyamlgoogle-cloud-build

how to build a yaml file to run my python code


Hi everyone and sorry for the silly question but its my day 2 with Yaml.

  • Problem statement: I have a python code which runs for 12 mins, (so I cant use Cloud Function to automate it), hence using cloud build as a hack.
  • Steps done so far: I have my code in google cloud repository and I used cloud build to build an image and created a google cloud build trigger. Now I want to run the main.py python code each time I trigger the build ( which I will do using a Cloud Scheduler as described here
  • Folder structure (as shown in the image below)

Folder structure in cloud repo

  • cloudbuild.yaml which I managed to write so far
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.

Question : How do I add the step to call/execute 'my_function' inside main.py file ?

appreciate your help.


Solution

  • 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.
    

    Let's say I have a file main.py:

    def foo():
      return "bar"
    

    This actually can be simplified into:

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