[英]Compiling protobuf messages using python plugin within Google Cloud Build
I've been dealing with this problem for several weeks now and am in dire need of help.我已经处理这个问题几个星期了,我急需帮助。 So thanks in advance for any insight you might have into how to compile protobufs into pb2.py files such that they are accessible to the rest of your workspace during a Google Cloud Build.
因此,提前感谢您对如何将 protobufs 编译成 pb2.py 文件的任何见解,以便在 Google Cloud Build 期间您的工作区的 rest 可以访问它们。
Attempts so far:到目前为止的尝试:
I first tried to use the google-cloud-builders, protoc image .我首先尝试使用 google-cloud-builders, protoc image 。 I was able to successfully push the image to the builders project registry but I'm not sure if I was able to correctly install the python plugin.
我能够成功地将图像推送到构建器项目注册表,但我不确定我是否能够正确安装 python 插件。
Here is my cloud_build.yaml step:这是我的 cloud_build.yaml 步骤:
- name: gcr.io/eco-env-238021/protoc args: - --proto-path=./protos - --python_out=./protos -./protos/A.proto
I kept getting an error reading: failed: starting step container failed: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "protoc": executable file not found in $PATH: unknown
我一直收到错误阅读:
failed: starting step container failed: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "protoc": executable file not found in $PATH: unknown
Next I tried using the grpcio-tools python package which is pip installable to compile the protos.接下来,我尝试使用可安装的 grpcio-tools python package pip 来编译原型。 This was much more successful because I was actually able to generate the pb2.py files.
这要成功得多,因为我实际上能够生成 pb2.py文件。 My excitement was short-lived though.
不过,我的兴奋是短暂的。 During the cloud build, I call a test file which imports one of the pb2.py files, we'll call it A. Now, I'm getting a module not found error when A imports another pb2.py file, which I'll refer to as B. I've printed out the directory structure within the cloud build environment and both A and B exist and B is definitely accessible to A (they exist within the same package).
在云构建期间,我调用了一个导入 pb2.py 文件之一的测试文件,我们将其称为 A。现在,当 A 导入另一个 pb2.py 文件时,我遇到了一个模块未找到错误,我'我将称为 B。我已经打印出云构建环境中的目录结构,并且 A 和 B 都存在,并且 A 绝对可以访问 B(它们存在于同一个包中)。
Here is the directory structure:这是目录结构:
C:.
| cloudbuild.yaml
| __init__.py
|
+---protos
| A_pb2.py
| B_pb2.py
| __init__.py
|
+---tests
| test.py
Here is my cloud_build.yaml step:
- name: python:3.7
args: ["python", "-m", "grpc_tools.protoc", "-I", "./protos",
"--python_out=./protos", "./protots/A.proto"]
- name: python:3.7
args: ["python", "-m", "grpc_tools.protoc", "-I", "./protos",
"--python_out=./protos", "./protots/B.proto"]
- name: python:3.7
args: ["python","-m","unittest","discover","--verbose","-s","./tests/",
"-p","test.py"]
id: unittest
The module import error is likely completely unrelated to the compilation of the protobuf files and simply a property of the cloud build environment.模块导入错误可能与 protobuf 文件的编译完全无关,而只是云构建环境的一个属性。 Something I did notice though, is that if I precompile the protobuf files within the repository that triggers the cloud build, everything works correctly.
不过我确实注意到,如果我在触发云构建的存储库中预编译 protobuf 文件,一切都会正常工作。 Or if I follow the same process on my PC everything works as well.
或者,如果我在我的 PC 上执行相同的过程,一切都会正常进行。 I just don't understand how the files can exist but not be importable.
我只是不明白文件如何存在但不可导入。
Update: I solved this by making my directory of protobufs (ie protos in this example) a pip installable package because pip installs seems to correctly add packages to PATH in cloud build environment.更新:我通过使我的 protobufs 目录(即本例中的 protos)成为 pip 可安装 package 来解决这个问题,因为 pip 安装似乎正确地将包添加到云构建环境中的 PATH。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.