是否可以将Go供应商库与Google App Engine一起使用?

时间:2017-07-09 13:15:27

标签: google-app-engine go glide-golang

我正在尝试将小型测试应用部署到Google App Engine(标准版)。如果我包含任何供应商库,我会收到错误。

这是我在尝试部署

时遇到的错误
% gcloud app deploy
Services to deploy:

descriptor:      [/Users/matt/work/appenginetest1/src/hello/default/app.yaml]
source:          [/Users/matt/work/appenginetest1/src/hello/default]
target project:  REDACTED
target service:  [default]
target version:  [20170709t220721]
target url:      REDACTED


Do you want to continue (Y/n)?  y

Beginning deployment of service [default]...
╔════════════════════════════════════════════════════════════╗
╠═ Uploading 0 files to Google Cloud Storage                ═╣
╚════════════════════════════════════════════════════════════╝
File upload done.
Updating service [default]...failed.
ERROR: (gcloud.app.deploy) Error Response: [9] Deployment contains files that cannot be compiled: Compile failed:
go-app-builder: build timing: 2×compile (210ms total), 0×link (0s total)
go-app-builder: failed running compile: exit status 2

main.go:6: can't find import: "github.com/julienschmidt/httprouter"

对于某些上下文,这是$ GOPATH的树

% tree $GOPATH
/Users/matt/work/appenginetest1
└── src
    └── hello
        ├── default
        │   ├── app.yaml
        │   └── main.go
        ├── glide.lock
        ├── glide.yaml
        └── vendor
            └── github.com
                └── julienschmidt
                    └── httprouter
                        ├── path.go
                        ├── router.go
                        └── tree.go

通过dev_appserver.py运行本地服务器可以正常工作。它看起来不像应用程序引擎在运行

后找不到供应商目录的情况
% rm -rf ~/work/appenginetest1/src/hello/vendor/github.com

在推送到云之前出现错误

% gcloud app deploy
ERROR: (gcloud.app.deploy) Staging command [/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/platform/google_appengine/goroot-1.6/bin/go-app-stager /Users/matt/work/appenginetest1/src/hello/default/app.yaml /var/folders/nx/8w2_6q551cl50h3ff6lmy9s40000gn/T/tmp97Kiis/tmpe0MHQ0] failed with return code [1].

------------------------------------ STDOUT ------------------------------------
------------------------------------ STDERR ------------------------------------
2017/07/09 22:12:52 failed analyzing /Users/matt/work/appenginetest1/src/hello/default: cannot find package "github.com/julienschmidt/httprouter" in any of:
    /Users/matt/work/appenginetest1/src/hello/vendor/github.com/julienschmidt/httprouter (vendor tree)
    ($GOROOT not set)
    /Users/matt/work/appenginetest1/src/github.com/julienschmidt/httprouter (from $GOPATH)
GOPATH: /Users/matt/work/appenginetest1
--------------------------------------------------------------------------------

如果我将github.com目录从供应商移动到src,则部署工作没有问题。

的app.yaml

service: default
runtime: go
api_version: go1


handlers:
- url: /.*
  script: _go_app

main.go

package hello

import (
  "fmt"
  "net/http"
  "github.com/julienschmidt/httprouter"
)

func init() {
  router := httprouter.New()
  router.GET("/hello/:name", Hello)
  http.Handle("/", router)
}

func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  fmt.Fprintf(w, "Hello, %s!", ps.ByName("name"))
}

如果我在运行滑动安装后将github.com目录从vendor移动到src,我可以部署这个项目。

2 个答案:

答案 0 :(得分:0)

您可以使用$GOPATH变量解决此问题。请注意,您的$GOPATH环境变量实际上可以是多个地方的列表(有关详情,请参阅https://golang.org/cmd/go/#hdr-GOPATH_environment_variable):

  

GOPATH环境变量列出了要查找Go代码的位置。上   Unix,值是冒号分隔的字符串。在Windows上,值为   以分号分隔的字符串。在计划9中,值是一个列表。

您可以编写一个脚本,暂时将vendor/github.com目录添加到您的$GOPATH环境变量,执行部署,然后将其删除,而不是将github.com目录从供应商移动到src。环境变量。

答案 1 :(得分:0)

根据rudolph1024的回答,我用来将项目推送到App Engine标准的一种方法是在make文件中添加几行。最简单的例子是:

GOPATH=./vendor gcloud app deploy

这具有使您可以使用here中的Go社区标准存储库布局而不是Google的here

的好处。

app引擎标准将app.yaml放在根目录中,并将使用该目录下的所有go文件。所以你可以做类似的事情

cp -r cmd/ ../../cmd  && cd ../../cmd && GOPATH=./vendor gcloud app deploy 

有很多方法可以解决此问题,但这是可行的,并提供了有关方法的总体思路。