I want to create a CI/CD pipeline with google Cloud Build to deploy a python web app to App Engine.
I have a tests.py
file that use some third-party library to run some tests.
I want Cloud Build to run the tests before deploying the app on App Engine. To achieve this I created this cloudbuild.yaml
file that install some packages with pip in the lib
folder of the /workspace working directory, run the tests, and deploy the app on app engine :
steps:
- name: "docker.io/library/python:3.7"
args: ['pip', 'install', '-t', '/workspace/lib', '-r', 'requirements.txt']
- name: 'docker.io/library/python:3.7'
args: ["python", "tests.py"]
- name: 'gcr.io/cloud-builders/gcloud'
args: ['app', 'deploy']
However I struggle to access the packages installed by pip in /workspace/lib from the import statements of the tests
module because /workspace/lib isn’t in the $PATH environment variable. I didn’t find a way to access the PATH environment variable of the cloud builder context from this config file so what I’m doing for now is adding /workspace/lib to the path in the beginning of my python file with the sys.path instruction.
import sys
sys.path.append("/workspace/lib")
Is there a better way to run a test step in Cloud Build that requires the installation of packages with pip ?
I found a better way to do that using the PYTHONPATH environment variable that can be set to /workspace/lib
for the step that is running the tests.
steps:
- name: "docker.io/library/python:3.7"
args: ['pip', 'install', '-t', '/workspace/lib', '-r', 'requirements.txt']
- name: 'docker.io/library/python:3.7'
args: ["python", "tests.py"]
env: ["PYTHONPATH=/workspace/lib"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With