在CI/CD GitLab流水线中,如何从proto文件生成pb文件?

huangapple go评论95阅读模式
英文:

how to generate pb file from proto files in ci/cd gitlab pipeline?

问题

我正在处理 CI/CD 流水线(GitLab)构建的工作,其中我需要在 CI/CD 流水线上从 proto 文件生成 pb 文件。
为了安装 proto 编译器,有以下几种方式:

  1. apt-get update && apt-get install -y protobuf-compiler
  2. go get -u github.com/golang/protobuf/protoc-gen-go
    但是在这两种方式中,我都遇到了以下错误:

protoc-gen-go: 找不到程序或程序不可执行
请指定一个绝对路径的程序,或确保该程序在您的 PATH 系统变量中可用
--go_out: protoc-gen-go: 插件返回状态码 1,失败。

英文:

I am working on CI/CD pipeline (GitLab) build where I need to generate pb files from proto files over CI/CD pipeline.
So in order to install proto compiler all below ways
1.apt-get update && apt-get install -y protobuf-compiler
2. go get -u github.com/golang/protobuf/protoc-gen-go
But in both ways I am getting error below error.

protoc-gen-go: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
--go_out: protoc-gen-go: Plugin failed with status code 1.

答案1

得分: 2

我认为你需要在你的 CI/CD 流水线中安装 protoc。

apt-get update
apt-get install -y protobuf-compiler
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2

执行以下命令获取环境路径:

go env

查找 GOENV="{/path}"。复制该路径并粘贴到下面的 {} 中:

export PATH="$PATH:{/path}"
英文:

I think you need to install protoc in your CI/CD pipeline.

apt-get update
apt-get install -y protobuf-compiler
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2

Hit the below command to get env path

go env

Look for GOENV="{/path}". Copy the path and paste it inside {} below :-

export PATH="$PATH:${/path}"

答案2

得分: 0

如果你正在从.proto定义生成Go文件,那么在Go中的常见做法是将这些文件提交到你的代码库中,这样如果你从CI/CD构建或从代码库创建模块时,它们可以被适当地打包起来。

如果你真的不想提交这些文件,那么你应该有一个很好的异常用例来解释这一点。

英文:

CAVEAT: this is alternative thought to your question as it has already be answered and approved.


If your are generating Go files from .proto definitions, then it is common practice in Go to commit those to your repo so that they can be packaged up appropriately if you are building from your CI/CD or creating a module from the repo.

It seems like if you really don't want to commit those files, then you should have a good outlier use case to that end to explain it back.

huangapple
  • 本文由 发表于 2023年3月11日 02:29:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75699795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定