首页 > 解决方案 > Google App Engine Flex Container Deployment Issues

问题描述

I am trying to deploy my Go 1.14 microservices application on to Google's Flexible environment. I read that there are issues with finding GOROOT, and how it is unable to obtain the correct dependencies.

I wanted to use the flexible environment because I needed to do port forwarding. Since my domain name was used to run the actual application, I wanted port 8081 to run my microservices.

I followed the instruction from this link:

https://blog.cubieserver.de/2019/go-modules-with-app-engine-flexible/

I tried option 3. This is my gitlab-ci.yaml configurations file.

# .gitlab-ci.yaml
stages:
  - build
  - deploy

build_server:
  stage: build
  image: golang:1.14
  script:
    - go mod vendor
    - go install farmcycle.us/user/farmcycle
  artifacts:
    paths:
      - vendor/

deploy_app_engine:
  stage: deploy 
  image: google/cloud-sdk:270.0.0
  script:
    - echo $SERVICE_ACCOUNT > /tmp/$CI_PIPELINE_ID.json 
    - gcloud auth activate-service-account --key-file /tmp/$CI_PIPELINE_ID.json
    - gcloud --quiet --project $PROJECT_ID app deploy app.yaml
    
  after_script:
  - rm /tmp/$CI_PIPELINE_ID.json

This my app.yaml configuration file

runtime: go
env: flex

network:
  forwarded_ports:
    - 8081/tcp

When I deployed this using the Git CI pipeline. Build stage passes, but the Deploy stage failed.

Running with gitlab-runner 13.4.1 (e95f89a0)
  on docker-auto-scale 72989761
Preparing the "docker+machine" executor
Preparing environment
00:03
Getting source from Git repository
00:04
Downloading artifacts
00:02
Executing "step_script" stage of the job script
00:03
$ echo $SERVICE_ACCOUNT > /tmp/$CI_PIPELINE_ID.json
$ gcloud auth activate-service-account --key-file /tmp/$CI_PIPELINE_ID.json
Activated service account credentials for: [farmcycle-hk1996@appspot.gserviceaccount.com]
$ gcloud --quiet --project $PROJECT_ID app deploy app.yaml
ERROR: (gcloud.app.deploy) Staging command [/usr/lib/google-cloud-sdk/platform/google_appengine/go-app-stager /builds/JLiu1272/farmcycle-backend/app.yaml /builds/JLiu1272/farmcycle-backend /tmp/tmprH6xQd/tmpSIeACq] failed with return code [1].
------------------------------------ STDOUT ------------------------------------
------------------------------------ STDERR ------------------------------------
2020/10/10 20:48:27 staging for go1.11
2020/10/10 20:48:27 Staging Flex app: failed analyzing /builds/JLiu1272/farmcycle-backend: cannot find package "farmcycle.us/user/farmcycle/api" in any of:
    ($GOROOT not set)
    /root/go/src/farmcycle.us/user/farmcycle/api (from $GOPATH)
GOPATH: /root/go
--------------------------------------------------------------------------------
Running after_script
00:01
Running after script...
$ rm /tmp/$CI_PIPELINE_ID.json
Cleaning up file based variables
00:01
ERROR: Job failed: exit code 1

This was the error. Honestly I am not really sure what this error is and how to fix it.

标签: gogcloud

解决方案


令人惊讶的是,即使使用最新的 Go 运行时,runtime: go1.15似乎也没有使用 go 模块。请参阅golang-docker

但是,Flex 无论如何都会将您的应用程序构建到一个容器中runtime,因此在这种情况下,使用自定义运行时并构建您自己的 Dockerfile 可能会更好?

runtime: custom
env: flex

然后你就可以使用 egGo 1.15和 go 模块(没有 vendoring)和任何你想要的东西。main.go对于使用模块的简单示例,例如:

FROM golang:1.15 as build

ARG PROJECT="flex"
WORKDIR /${PROJECT}

COPY go.mod .
RUN go mod download

COPY main.go .

RUN GOOS=linux \
    go build -a -installsuffix cgo \
    -o /bin/server \
    .

FROM gcr.io/distroless/base-debian10

COPY --from=build /bin/server /

USER 999

ENV PORT=8080
EXPOSE ${PORT}

ENTRYPOINT ["/server"]

这应该可以通过谷歌最近宣布的对 buildpacks 的支持来实现,但我还没有尝试过。


推荐阅读