Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good practice to run python tests in the Cloud Build pipeline?

Tags:

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 ?

like image 449
Mathieu Rollet Avatar asked Jul 09 '19 04:07

Mathieu Rollet


1 Answers

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"]
like image 60
Mathieu Rollet Avatar answered Oct 02 '22 16:10

Mathieu Rollet