登录
首页 >  Golang >  Go问答

无法在 Oracle Cloud Function 中用 Cloud Shell 部署 Go 编写的函数

来源:stackoverflow

时间:2024-03-17 22:21:31 335浏览 收藏

在 Oracle Cloud Shell 中部署使用 Go 编写的 Cloud Function 时遇到困难。使用 Java 运行时可以成功部署,但使用 Go 运行时时总是返回错误。问题似乎与 cloudshell 中的权限问题有关,阻止 Go 构建过程查找必要的模块。作为临时解决方案,建议在 gopath 目录中手动设置环境变量,以解决权限问题。

问题内容

我尝试按照官方文档说明创建和部署 oracle cloud functions。我可以使用 java 运行时创建和部署,但是当我部署 go 运行时时总是返回错误。

我尝试在 oracle cloud shell 中使用以下命令初始化 go 函数:

fn init --runtime go hello-go

然后我尝试部署它

fn -v deploy --app test

但它返回了如下错误:

deploying hello-go to app: test
bumped to version 0.0.7
building image bom.ocir.io/bmptwl2psusa/repo/hello-go:0.0.7 
fn_registry:  bom.ocir.io/bmptwl2psusa/repo
current context:  ap-mumbai-1
sending build context to docker daemon  5.632kb
step 1/10 : from fnproject/go:dev as build-stage
 ---> 96c8fb94a8e1
step 2/10 : workdir /function
 ---> using cache
 ---> 8961dd299ec1
step 3/10 : workdir /go/src/func/
 ---> using cache
 ---> 5a4c2c6e13f1
step 4/10 : env go111module=on
 ---> using cache
 ---> 22022ff2fcf8
step 5/10 : copy . .
 ---> 714622a6ff03
step 6/10 : run cd /go/src/func/ && go build -o func
 ---> running in 39fedbc476f4
build func: cannot find module for path github.com/fnproject/fdk-go
the command '/bin/sh -c cd /go/src/func/ && go build -o func' returned a non-zero code: 1


fn: error running docker build: exit status 1

当我使用 java 运行时和 fn init --runtime java hello-java 命令时,部署成功,为什么使用 go 时总是失败?

我尝试在 hello-go 目录中运行 go build -o func 但它返回了:

go: finding module for package github.com/fnproject/fdk-go
go: writing stat cache: mkdir /usr/share/gocode/pkg: permission denied
go: downloading github.com/fnproject/fdk-go v0.0.3
func.go:10:2: mkdir /usr/share/gocode/pkg: permission denied

我知道发生这种情况是因为 /usr/share/gocode/ 目录位于 root 用户下,但我不知道如何更改该文件夹的权限,因为 oracle cloud shell 无法使用 root 用户或 sudo。 (基于这个答案)

如果我使用真正的vm shell或本地shell/终端,也许我可以做到这一点,但我想使用oracle cloud shell,因为我只是按照建议我使用oracle cloud shell的官方说明进行操作,那么如何部署oracle cloud functions使用 oracle cloud shell 运行时?

官方文档大多只给出了使用java运行时的示例,这让我在使用go时变得偏执。


解决方案


这是 cloudshell 中的一个错误,我们正在寻找解决该问题的最佳方法。

作为短期解决方法,您可以执行一次此操作:

mkdir ${home}/gopath

然后在您的终端中进行设置:

export GOPATH=${HOME}/gopath

您可能应该编辑 ~/.bashrc 以自动设置 gopath 变量,这样您就不会忘记

到这里,我们也就讲完了《无法在 Oracle Cloud Function 中用 Cloud Shell 部署 Go 编写的函数》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>