如何使用go 1.11和Google App Engine标准对私有Go模块进行身份验证

时间:2018-12-07 05:09:45

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

我一直在更新我的整个go gae标准项目,以使用go 1.11的模块。

主目录结构

app.yaml
app.go
go.mod
go.sum

app.go

package main

import "bitbucket.org/myPrivateRepo"

func main() {
    myImportantModule.Run()
}

go.mod

module myProject

require bitbucket.org/myPrivateRepo v0.0.1

错误

如果我尝试gcloud应用部署:

ERROR: (gcloud.app.deploy) Error Response: [9] Cloud build <GUI> 
status: FAILURE.
Build error details: go: bitbucket.org/myPrivateRepo@v0.0.1: 
https://api.bitbucket.org/2.0/repositories/myPrivateRepo?fields=scm: 
403 Forbidden

(注意:显然,我正在使用的存储库具有真实名称)。

那么我可以这样吗?我承认我不会完全理解迁移文档,尤其是在谈到“将文件移动到GOPATH”时。 https://cloud.google.com/appengine/docs/standard/go111/go-differences

我的意思是,我认为新模块系统的好处之一是您并不需要一切。例如,当我阅读https://github.com/golang/go/wiki/Modules时,它很早就说“在GOPATH之外创建目录:”

因此,很明显,现在我所有的代码都在执行路径之外,但是一切都在本地构建。

我认为这都是可行的,因为当我运行go mod tidy / go build等时,go会自动在go路径中下载并缓存内容。

但是,当我尝试gcloud app部署时,它失败了。 Google云构建系统将如何访问我的私有存储库?我显然错过了一些重要的事情。我还读到您不应该将供应商与新的模块系统结合起来,所以不可能。

如果这行得通,我将非常高兴,因为使用DEP迫使我尴尬地使用goapp部署。

谢谢!

3 个答案:

答案 0 :(得分:1)

我的解决方案:

我没有使用凭据,而是使用go的模块替换功能来指向GAE以使用我的本地代码。效果很好。

目录结构:

myService/
    src/
        service.go  // has a run() function to set up routers etc.
        go.mod      // depends on my private module in bitbucket and other things
        …           // other source files
    build/
        gae/
            src/        // simlink to ../../src
            modules/    // git ignored, I clone or copy my modules in build scripts.
            app.go  // see below…
            go.mod  // has main() which calls service.run() and appEngine.Main()
            app.yaml

方法

我使用git模块替换,以便GAE使用我的本地代码。在构建之前,我解析myService / src / go.mod来找到我的私有模块的正确版本,然后将其克隆到modules文件夹中。我还选择了复制wip模块源代码以进行本地调试,而无需提交到我的模块存储库。

gae目录中的

go.mod:

module myServiceGAE

require (
    bitbucket.org/me/myService v0.0.0
    google.golang.org/appengine v1.4.0
)

replace bitbucket.org/me/myService => ./src

replace bitbucket.org/me/myModule => ./modules/utils

专业人士

myService下的程序包没有GAE的参考资料或知识,因此我可以轻松地将其构建到docker等中。我认为解析服务go.mod文件就像创建我自己的依赖项管理器一样,克服了go模块的好处。

缺点

如果我有一个依赖于另一个私有模块的私有模块,我认为事情会变得太复杂了。

答案 1 :(得分:0)

另一种替代方法是也使用Google Cloud Secret Manager

https://cloud.google.com/cloud-build/docs/access-private-github-repos

Google Cloud将具有SSH密钥来访问和提取您的私有存储库。

答案 2 :(得分:-3)

在部署之前设置git凭据:

git config credential.helper '!f() { sleep 1; echo "username=${GIT_USER}\npassword=${GIT_PASSWORD}"; }; f'

export GIT_USER=put_git_user_here
export GIT_PASSWORD=put_git_password_here

gcloud app deploy