dongyouzhui1969 2019-03-20 05:25
浏览 44
已采纳

在gcp cloudbuild中使用`replace`指令失败

I'm trying to build my go application using serverless framework in cloudbuild.

Here's my project structure

/api
   /giam
      go.mod
      API.go
   /other_folders...
/util
   go.mod
   util.go

Here's my build steps:

steps:
- name: 'gcr.io/cloud-builders/npm'
  args: ['install']
- name: 'softinstigate/serverless'
  args: ['deploy', '-v']
  dir: 'api/giam'
  env: ['PROJECT_ROOT=${REPO_NAME}', 'GO111MODULE=on']

and here's my go.mod in api/giam

module mybackend

require bitbucket.org/myusername/mybackend/util v0.0.0

replace bitbucket.org/myusername/mybackend/util => /workspace/util

and I confirmed that I have a go.mod in my util folder.

and here's the content

module util

require cloud.google.com/go v0.37.1

The error

I got this error while running the cloudbuild..

{"ResourceType":"cloudfunctions.v1beta2.function","ResourceErrorCode":"400","ResourceErrorMessage":"Build failed: go: parsing util/go.mod: open /workspace/util/go.mod: no such file or directory
go: error loading module requirements
"}

if I got this right, the error is that it cannot find the /workspace/util/go.mod

I tried ls in the cloudbuild, specifically in /workspace/util and I found the go.mod file.

I'm pretty stuck here.. I don't know what to do next..

  • 写回答

1条回答 默认 最新

  • duanchuo7741 2019-03-20 13:04
    关注

    I managed to get it working by using vendor and omitting go.mod and go.sum in serverless package (by using package.exclude).

    Here's my updated cloudbuild.yaml:

    steps:
    - name: 'gcr.io/cloud-builders/npm'
      args: ['install']
    - name: 'gcr.io/cloud-builders/go'
      dir: 'api/giam'
      args: ['mod', 'edit', '-replace', 'bitbucket.org/myusername/mybackend/util=/workspace/util']
      env: ['GOPATH=/src', 'GO111MODULE=on']
    - name: 'gcr.io/cloud-builders/go'
      dir: 'api/giam'
      args: ['mod', 'vendor']
      env: ['GOPATH=/src', 'GO111MODULE=on']
    - name: 'softinstigate/serverless'
      args: ['deploy', '-v']
      dir: 'api/giam'
      env: ['GOPATH=/src', 'GO111MODULE=on']
    

    and my go.mod:

    module mybackend
    
    require (
        bitbucket.org/myusername/mybackend/util v0.0.0
        google.golang.org/api v0.2.1-0.20190318183801-2dc3ad4d67ba
    )
    
    replace bitbucket.org/myusername/mybackend/util => ../../util
    

    as per github issue in https://github.com/GoogleCloudPlatform/golang-samples/issues/743, it says that you need to exclude go.mod and go.sum in the deployment, otherwise the vendor won't be used. That's why in serverless.yml I use:

    package:
      exclude:
        - ./**
      include:
        - vendor/**
        - '*.go'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛