$GOPATH/go.mod 存在但不应该在 AWS Elastic Beanstalk 中 [英] $GOPATH/go.mod exists but should not in AWS Elastic Beanstalk
问题描述
我正在尝试使用 bitbucket 管道和 AWS Elastic Beanstalk 部署基于 gin 框架的 golang 应用程序.我使用在 EB 中部署应用程序所需的所有文件创建了我的包 application.zip,当我通过管道或 AWS 控制台中的手动菜单推送它时,我得到了这些:
I'm trying to deploy a golang app based on gin framework using bitbucket pipeline and AWS Elastic Beanstalk. I create my package application.zip with all the files needed to deploy the app inside an EB, when I push it, both by pipeline or by manual menu in AWS console I got these:
17:21:49 make.1 | go: finding github.com/appleboy/gin-jwt v2.5.0+incompatible
17:21:49 make.1 | go: downloading github.com/appleboy/gin-jwt v2.5.0+incompatible
17:21:49 make.1 | go: extracting github.com/appleboy/gin-jwt v2.5.0+incompatible
17:21:50 make.1 | go: finding gopkg.in/dgrijalva/jwt-go.v3 v3.2.0
17:21:50 make.1 | go: downloading gopkg.in/dgrijalva/jwt-go.v3 v3.2.0
17:21:50 make.1 | go: extracting gopkg.in/dgrijalva/jwt-go.v3 v3.2.0
17:21:50 make.1 | + GOOS=linux
17:21:50 make.1 | + GOARCH=amd64
17:21:50 make.1 | + go build -o bin/application '-ldflags=-s -w'
17:21:50 make.1 | go: cannot find main module; see 'go help modules'
17:21:50 make.1 | exited with code 1
17:21:50 system | sending SIGTERM to all processes
我看到我忘记了 go.mod 文件,确实添加了它然后我收到了这些消息并且一切正常:
I see I forgot the go.mod file, indeed adding it then I got these message and everything works great:
17:27:59 make.1 | go: extracting github.com/appleboy/gin-jwt v2.5.0+incompatible
17:27:59 make.1 | go: downloading gopkg.in/dgrijalva/jwt-go.v3 v3.2.0
17:27:59 make.1 | go: extracting gopkg.in/dgrijalva/jwt-go.v3 v3.2.0
17:27:59 make.1 | go: finding gopkg.in/dgrijalva/jwt-go.v3 v3.2.0
17:27:59 make.1 | + GOOS=linux
17:27:59 make.1 | + GOARCH=amd64
17:27:59 make.1 | + go build -o bin/application '-ldflags=-s -w'
17:27:59 make.1 | go: downloading golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c
17:28:00 make.1 | go: extracting golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c
17:28:00 make.1 | go: finding golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c
17:28:01 make.1 | exited with code 0
17:28:01 system | sending SIGTERM to all processes
但是在工作时我对代码进行了一些更改,然后我再次提交,认为现在一切都很好,但我得到了这些日志行:
But then while working I made some changes in my code, and I commit again, thinking that now everything is fine but I got these logs line:
Executing: HOME=/tmp /opt/elasticbeanstalk/lib/ruby/bin/ruby /opt/elasticbeanstalk/lib/ruby/bin/foreman start --procfile /tmp/d20191202-5748-15rj5l8/eb-buildtask-0 --root /var/app/staging --env /var/elasticbeanstalk/staging/elasticbeanstalk.env
17:32:00 make.1 | started with pid 5761
17:32:00 make.1 | + go get github.com/gin-gonic/gin
17:32:00 make.1 | $GOPATH/go.mod exists but should not
17:32:00 make.1 | exited with code 1
17:32:00 system | sending SIGTERM to all processes
现在我迷失了 $GOPATH/go.mod 存在但不应该我使用的是最新的 Go 版本 1.13.4,而 AWS 使用的是 1.13.2.
And now I'm lost about $GOPATH/go.mod exists but should not I'm using last Go vesion 1.13.4, and AWS uses 1.13.2.
更多信息
我会尽量对项目进行更准确的描述,如果日志不够,请见谅.我有一个带有 gorm + gin 的简单应用程序,它基本上是一个 REST API 服务器.我的项目结构是这样的:
I'll try to be more precise about the project, sorry if the logs is not enough. I have a simple application with gorm + gin, it's basically a REST API server. the structure of my project is like this:
/go/src/company.com/project
/go/src/company.com/project
里面有这些文件和文件夹:
Inside I have these files and folders:
application.go
go.mod
Controller/dashboardController.go
Model/user.go
Service/utility.go
BuildFile
Procfile
最后两个文件用于让EB构建项目一次上传到S3.当然,本地一切正常,以及我第一次上传包含 go.mod 的完整项目时,请参阅第二组日志.但是当我修改一个文件,然后我再次打包应用程序并将其发送到 EB 时,无论是通过 bitbucket piple 还是 aws 控制台,都没有关系,我收到了错误.
Last two files are used to let the EB build the project once is uploaded in the S3. Of course locally everything works just fine, as well as the first time I upload the complete project with go.mod included, see the second bunch of logs. But when I amend a file, and I pack the application again and send it to EB, both via bitbucket piple or aws console, it doesn't matter, I got the error.
解决方案
经过多次头痛,我决定通过 ssh 登录 ElasticBeanstalk,我发现如果将下面的行添加到我的 build.sh 脚本中,所有问题都会消失:
After a lot of headhache I decide to log into ElasticBeanstalk via ssh, and I found that if I add to my build.sh script the line below than all the problems go away:
sudo rm /var/app/current/go.*
我最后的build.sh文件是这样的:
My build.sh file at the end is like this:
#!/usr/bin/env bash
# Stops the process if something fails
set -xe
touch /var/app/current/go.bak
sudo rm /var/app/current/go.*
# get all of the dependencies needed
go get "github.com/gin-gonic/gin"
go get "github.com/jinzhu/gorm"
go get "github.com/jinzhu/gorm/dialects/postgres"
go get "github.com/appleboy/gin-jwt"
# create the application binary that eb uses
GOOS=linux GOARCH=amd64 go build -o bin/application -ldflags="-s -w"
推荐答案
当设置 GOPATH
时 go get
将请求的模块安装到 GOPATH 中提供的路径
但如果你使用 .mod
文件,它会使用工作目录.
When GOPATH
is set go get
installs the requested module to the path provided in GOPATH
but if you use .mod
file, it uses the working directory.
你的情况,你有 GOPATH
设置和 .mod
文件.
Your case, you have both GOPATH
set and have the .mod
file.
您可以取消设置
GOPATH,例如:
You can unset
GOPATH like:
unset GOPATH
它应该可以解决您的问题.或者,您也可以删除 .mod
文件.
It should solve your problem. Or, you can delete .mod
file too.
这篇关于$GOPATH/go.mod 存在但不应该在 AWS Elastic Beanstalk 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!