我正在尝试将一个小型测试应用程序部署到 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,则部署工作没有问题。
应用程序.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"))
}
如果在运行 glide install 后将 github.com 目录从供应商移动到 src,我可以部署这个项目。