python - 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.
解决方案
我相信问题出在你的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 ===========================
推荐阅读
- jira - Jira - 创建 rest api 查询
- r - 比较箱线图分布中的集群
- ios - 使委托方法对其他类不可见
- python - 如何计算所有分类标签之间的分布并将总出现次数存储在列表中
- r - !LaTeX 错误:找不到文件“knitr.sty”
- cordova - 无法构建空项目“任务':app:mergeDebugResources'的执行失败”
- angular - 将元素的 ngClass 条件绑定到它的父 div 属性
- python - 如何绘制二次函数?
- python - 名称“事件”未定义不知道为什么我得到这个
- pdf - 如果使用 itextsharp 对 pdf 进行密码保护,因为 isencrypted 在最新库中为假,如何处理?