You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

构建Cloud Functions(Gen2)时拉取GCP私有Go包失败求助

Cloud Functions部署无法拉取Artifact Registry私有Go模块问题

Cloud Function代码示例

package main

import (
    "encoding/json"
    "fmt"
    "html"
    "net/http"

    "example.com/boomer"
    "github.com/GoogleCloudPlatform/functions-framework-go/functions"
)

func helloHTTP(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, boomer.Bang())
    var d struct {
        Name string `json:"name"`
    }
    if err := json.NewDecoder(r.Body).Decode(&d); err != nil {
        fmt.Fprint(w, "Hello, World!")
        return
    }
    if d.Name == "" {
        fmt.Fprint(w, "Hello, World!")
        return
    }
    fmt.Fprintf(w, "Hello, %s!", html.EscapeString(d.Name))
}

func main() {
    functions.HTTP("HelloHTTP", helloHTTP)
}

// helloHTTP is an HTTP Cloud Function with a request parameter.

Cloud Build配置文件

steps:
  - name: golang
    entrypoint: go
    args: [ 'run', 'github.com/GoogleCloudPlatform/artifact-registry-go-tools/cmd/auth@v0.1.0', 'add-locations', '--locations=${_LOCATION}' ]
    env:
      # Set GOPROXY to the public proxy to pull the credential helper tool
      - 'GOPROXY=proxy.golang.org'

  - name: golang
    entrypoint: go
    args: [ 'run', 'github.com/GoogleCloudPlatform/artifact-registry-go-tools/cmd/auth@v0.1.0', 'refresh' ]
    env:
      - 'GOPROXY=proxy.golang.org'
      - 'GONOPROXY=github.com/GoogleCloudPlatform/artifact-registry-go-tools'

  - name: golang
    entrypoint: go
    args: [ 'run', '.' ]
    env:
      - 'GONOPROXY=github.com/GoogleCloudPlatform/artifact-registry-go-tools'
      - 'GONOSUMDB=*.${_MODULE_PATH}'
      - 'GOPRIVATE=*.${_MODULE_PATH}'

# Deploy
  - name: gcr.io/google.com/cloudsdktool/cloud-sdk
    args:
      - gcloud
      - functions
      - deploy
      - hello-go
      - --allow-unauthenticated
      - --region=us-central1
      - --source=.
      - --trigger-http
      - --runtime=go121
      - --entry-point=HelloHTTP
      - --set-build-env-vars=^;^GONOSUMDB="${_MODULE_PATH}";GOPRIVATE="${_MODULE_PATH}";GONOPROXY="github.com/GoogleCloudPlatform/artifact-registry-go-tools";GOPROXY="${_LOCATION}-go.pkg.dev/${_PROJECT_ID}/${_REPOSITORY},https://proxy.golang.org,direct"

options:
  env:    # Disable GO sumdb checks for private modules.
    - 'GONOPROXY=github.com/GoogleCloudPlatform/artifact-registry-go-tools'
    - 'GOPROXY=${_LOCATION}-go.pkg.dev/${_PROJECT_ID}/${_REPOSITORY},https://proxy.golang.org,direct'
    - 'GONOSUMDB=${_MODULE_PATH}'
    - 'GOPRIVATE=${_MODULE_PATH}'

错误日志

severity: "INFO"
textPayload: "Step #2 - \"build\":    github.com/pkg/errors: example.com/boomer@v0.0.16: unrecognized import path \"example.com/boomer\": reading https://example.com/boomer?go-get=1: 404 Not Found"
timestamp: "2023-12-15T22:00:53.973146065Z"

问题说明

私有Go模块存放在Google Cloud Artifact Registry中,Cloud Function引用该模块。使用上述Cloud Build配置部署时,前序auth步骤均正常,但最终部署Cloud Functions的步骤持续报错——系统尝试直接访问example.com/boomer返回404,而非从配置的私有Artifact Registry拉取。

怀疑Cloud Build的登录凭证未传递给gcloud deploy触发的构建流程,不想使用私有构建池,希望得到调整配置的可行建议。


内容的提问来源于stack exchange,提问作者Eric E

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 02:35:01