gpt4 book ai didi

python - Google 云函数 : CI/CD for Python 3. 7 运行时

转载 作者:行者123 更新时间:2023-12-01 01:13:26 24 4
gpt4 key购买 nike

docs 的末尾关于测试云功能,有一个关于 CI/CD 的部分。然而,他们给出的唯一示例是针对节点的。我一直在尝试用 python 3.7 做一些事情,但没有成功。

每次推送到 Google Source Cloud 存储库时,我都会设置一个触发器。这是一个多功能项目

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

我已经尝试过 example进行定制构建。

这是我的cloudbuild.yml:

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

这是我的Dockerfile:

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

当我在云构建环境(而不是本地)中运行它时,出现以下错误:

"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

这意味着我没有足够的权限来读取自己的文件?我不确定我这样做是否正确。

最佳答案

我相信问题出在您的 cloudbuild.yaml 上。您需要对其进行配置以从 Dockerfile 构建镜像。 Check the official 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 构建提交 --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 ===========================

关于python - Google 云函数 : CI/CD for Python 3. 7 运行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54601826/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com