在App Engine标准构建失败上部署go1.11:您的应用不在GOPATH上

时间:2018-11-24 19:02:59

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

我尝试将Go应用程序部署到App Engine。我有以下构建错误:

Starting Step #1 - "builder"
Step #1 - "builder": Pulling image: gcr.io/gae-runtimes/go111_app_builder:go111_1_11_2_20181111_RC00
Step #1 - "builder": go111_1_11_2_20181111_RC00: Pulling from gae-runtimes/go111_app_builder
Step #1 - "builder": Digest: sha256:51fb36bfa16e7013356867c3a3972986084df93e56258fc258579a5799f0436e
Step #1 - "builder": Status: Downloaded newer image for gcr.io/gae-runtimes/go111_app_builder:go111_1_11_2_20181111_RC00
Step #1 - "builder": 2018/11/24 18:13:29 Your app is not on your GOPATH, this build may fail.
Step #1 - "builder": 2018/11/24 18:13:29 Building from Go source in /tmp/staging477638319/srv, with main package at ./...
Step #1 - "builder": 2018/11/24 18:13:29 Building /tmp/staging477638319/srv, saving to /tmp/staging477638319/usr/local/bin/start
Step #1 - "builder": 2018/11/24 18:13:30 Wrote build output to /builder/outputs/output
Step #1 - "builder": 2018/11/24 18:13:30 Failed to build app: Your app is not on your GOPATH, please move it there and try again.
Step #1 - "builder": building app with command '[go build -o /tmp/staging477638319/usr/local/bin/start ./...]', env '[PATH=/go/bin:/usr/local/go/bin:/builder/google-cloud-sdk/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=d253e517b16c HOME=/builder/home BUILDER_OUTPUT=/builder/outputs DEBIAN_FRONTEND=noninteractive GOROOT=/usr/local/go/ GOPATH=/go GOPATH=/tmp/staging477638319/srv/gopath]': err=exit status 1, out=srv/main.go:7:2: cannot find package "cloud.google.com/go/firestore" in any of:
Step #1 - "builder": /usr/local/go/src/cloud.google.com/go/firestore (from $GOROOT)
Step #1 - "builder": /tmp/staging477638319/srv/gopath/src/cloud.google.com/go/firestore (from $GOPATH)
Step #1 - "builder": srv/main.go:8:2: cannot find package "github.com/gin-gonic/gin" in any of:
Step #1 - "builder": /usr/local/go/src/github.com/gin-gonic/gin (from $GOROOT)
Step #1 - "builder": /tmp/staging477638319/srv/gopath/src/github.com/gin-gonic/gin (from $GOPATH)
Step #1 - "builder": srv/main.go:9:2: cannot find package "google.golang.org/api/option" in any of:
Step #1 - "builder": /usr/local/go/src/google.golang.org/api/option (from $GOROOT)
Step #1 - "builder": /tmp/staging477638319/srv/gopath/src/google.golang.org/api/option (from $GOPATH)
Finished Step #1 - "builder"
ERROR
ERROR: build step 1 "gcr.io/gae-runtimes/go111_app_builder:go111_1_11_2_20181111_RC00" failed: exit status 1

我的app.yaml文件如下:

runtime: go111
handlers:
- url: /api/user
  script: auto

- url: /favicon.ico
  static_files: build/favicon.ico
  upload: build/favicon.ico

- url: /
  static_files: build/index.html
  upload: build/index.html

- url: /
  static_dir: build

我在根目录下的main.go文件如下:

package main

import (
    "context"
    "net/http"

    "cloud.google.com/go/firestore"
    "github.com/gin-gonic/gin"
    "google.golang.org/api/option"
)

const firestoreAccountFile = "firebase.json"
const firestoreProjectID = "golang-gae-firestore-template"

type formData struct {
    Name  string `json:"name" binding:"required"`
    Email string `json:"email" binding:"required"`
}

func main() {
    // Gin init
    r := gin.Default()
    // Serve from static build directory
    r.StaticFS("/", http.Dir("./build"))
    // routes
    r.POST("/api/user", userHandler)
    // run application on port 8080
    r.Run(":8080")
}

func writeLogIfError(c *gin.Context, err error) {
    if err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    }
}

func getNewFirestoreClient(ctx context.Context) (*firestore.Client, error) {
    return firestore.NewClient(ctx, firestoreProjectID, option.WithServiceAccountFile(firestoreAccountFile))
}

func userHandler(c *gin.Context) {
    ctx := context.Background()

    client, err := getNewFirestoreClient(ctx)
    writeLogIfError(c, err)
    defer client.Close()
    // Get form data
    var form formData
    c.BindJSON(&form)

    // [START add user to firestore]
    _, _, err = client.Collection("users").Add(ctx, map[string]interface{}{
        "name":  form.Name,
        "email": form.Email,
    })
    if err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
        return
    }
    // [END add user to firestore]
    c.JSON(http.StatusOK, gin.H{"status": "user added to db"})
}

如果没有这些构建错误,我将无法成功构建。我试图完全遵循Go on App Engine的文档,如果我将应用程序的结构与建议的有所不同,我会感到困惑。对如何解决此GOPATH错误有何想法?

2 个答案:

答案 0 :(得分:1)

更新:我没有成功使用GOPATH进行部署,而是在包含env变量后成功使用go.mod进行了部署:export GO111MODULE = on使模块正常工作。这里的文档:https://github.com/golang/go/wiki/Modules

答案 1 :(得分:1)

它对我有用的方法是将我的应用放入GOPATH/src/文件夹-与第三方依赖项一起存放在该文件夹中。

https://cloud.google.com/appengine/docs/standard/go111/specifying-dependencies

说:

  

如果您的应用程序位于GOPATH环境变量上的目录中,则App Engine会分析,复制并上传您从GOPATH导入的内容

-这很令人困惑,即我首先尝试将我的应用程序放到GOPATH文件夹中,但它不起作用。

因此,假设您的GOPATH文件夹为C:\go,则项目的结构应如下所示:

- C
  - go
     - pkg
     - src
         - cloud.google.com
         - github.com
         - google.golang.org
         - your-app