doucaishou0074 2018-11-24 19:02
浏览 68

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

I have attempted to deploy my Go app to App Engine. I have the following build errors:

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

My app.yaml file looks like:

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

My main.go file at the root looks like:

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"})
}

I cannot build successfully without getting these build errors. I've attempted to follow the documentation for Go on App Engine exactly and am confused if I am to structure my application any different than suggested. Any thoughts on how to resolve this GOPATH error?

  • 写回答

1条回答 默认 最新

  • drxnfdx798517235 2018-11-27 21:16
    关注

    Update: I was unable to successfully deploy with GOPATH, and instead successfully was able to deploy with go.mod after I included the env variable: export GO111MODULE=on for the modules to work. Documentation here: https://github.com/golang/go/wiki/Modules.

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程