简体   繁体   中英

$GOPATH/go.mod exists but should not in AWS Elastic Beanstalk

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

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

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.

MORE INFO

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

Inside I have these files and folders:

application.go
go.mod
Controller/dashboardController.go
Model/user.go
Service/utility.go
BuildFile
Procfile

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.

SOLUTION

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.*

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"

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.

Your case, you have both GOPATH set and have the .mod file.

You can unset GOPATH like:

unset GOPATH

It should solve your problem. Or, you can delete .mod file too.

May be this can help you.

It looks like you have GOPATH=. and you are copying the module into that same place. The go get has the effect of creating ./src/golang.org/x/vgo, and then the COPY creates ./go.mod, so that it looks like the GOPATH's downloaded source lives inside the module. That in turn makes it look like src/golang.org/x/vgo is part of the project you are working on, and vgo gets very confused by its own (unsupported) use of vendoring.

Original post reference: here

Additional source : here

I had a similar problem and what I did was:

go env -u GOPATH

It should solve your problem if you had ever set the GOPATH variable like this go env -w GOPATH=some_path .

您必须删除位于 GOPATH 目录位置的 go.mod 文件

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM