1
votes

At the end of the docs on testing your cloud function there is a section on CI/CD. However, the only example they give is for node. I've been trying to do something with python 3.7 to no avail.

I set up a trigger for each time I push to a Google Source Cloud repository. Its a multifunction project

├── actions
│   ├── action.json
│   └── main.py
├── cloudbuild.yaml
├── Dockerfile
├── graph
│   ├── main.py
│   └── requirements.txt
└── testing
    ├── test_actions.py
    └── test_graph.py

I've tried the example to make a custom build.

here is my cloudbuild.yml:

steps:
  - name: 'gcr.io/momentum-360/pytest'

Here is my Dockerfile:

FROM python:3.7
COPY . /
WORKDIR /
RUN pip install -r graph/requirements.txt
RUN pip install pytest
ENTRYPOINT ["pytest"]

I'm getting the following error when I run it in the cloud build environment (not locally):

"Missing or insufficient permissions.","grpc_status":7}"
E >

The above exception was the direct cause of the following exception:
testing/test_graph.py:7: in <module>
from graph import main

which means I don't have enough permission to read my own files? I'm not sure I'm doing this right at all.

1

1 Answers

2
votes

I believe the problem is with your cloudbuild.yaml. You need to configure it to build the image from the Dockerfile. Check the official Google Cloud Build to know how to create a build configuration file.

I have been trying with this simple setup and it worked for me:

├── project
    ├── cloudbuild.yaml
    └── Dockerfile
    └── test_sample.py

The content of test_sample.py:

def inc(x):
   return x + 1

def test_answer():
   assert inc(3) == 4

Here is the cloudbuild.yaml:

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/pytest-image', '.' ]
images:
- 'gcr.io/$PROJECT_ID/pytest-image'

The Dockerfile, it is important to copy the project in its own directory to run pytest.

FROM python:3.7
COPY . /project
WORKDIR /project
RUN pip install pytest
ENTRYPOINT ["pytest"]

Now, inside the project directory we build the image:

gcloud builds submit --config cloudbuild.yaml .

We pull it:

docker pull gcr.io/$PROJECT_ID/pytest-image:latest

And run it:

docker run gcr.io/$PROJECT_ID/pytest-image:latest

Result:

============================= test session starts ==============================
platform linux -- Python 3.7.2, pytest-4.2.0, py-1.7.0, pluggy-0.8.1
rootdir: /src, inifile:
collected 1 item

test_sample.py .                                                         [100%]

=========================== 1 passed in 0.03 seconds ===========================