首页 > 解决方案 > Google Cloud Functions: CI/CD for Python 3.7 Runtime

问题描述

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.

标签: pythongoogle-cloud-platformgoogle-cloud-build

解决方案


我相信问题出在你的cloudbuild.yaml. 您需要对其进行配置以从 Dockerfile 构建映像。查看官方 Google Cloud Build以了解如何创建构建配置文件。

我一直在尝试这个简单的设置,它对我有用:

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

的内容test_sample.py

def inc(x):
   return x + 1

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

这是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'

Dockerfile重要的是把项目复制到自己的目录下运行pytest。

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

现在,在项目目录中,我们构建图像:

gcloud builds submit --config cloudbuild.yaml .

我们拉它:

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

并运行它:

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

结果:

============================= 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 ===========================

推荐阅读