gpt4 book ai didi

google-cloud-functions - Google Cloud build 总是使用先前的提交来部署我的云功能

转载 作者:行者123 更新时间:2023-12-03 20:55:07 25 4
gpt4 key购买 nike

我的自动化云功能部署有问题

我有一个存储在 Google Cloud 存储库中的云函数

Git 代码包含一个带有以下内容的 cloudbuild.yaml 文件:

steps:
- name: "gcr.io/cloud-builders/gcloud"
args: ["functions", "deploy", "myfunction", "--region=europe-west1"]
timeout: "1600s"

我只有一个分支Master。

当我推送我的提交时,cloudbuild 会触发并部署云功能

问题是它总是部署上一个提交,而不是最后一个

例如 :
2:23 :我将我的源代码推送到 Google 源代码库

结果如下:

Commit from local source

2:23:33,cloudbuild触发并部署成功云功能

enter image description here

这是 Cloudbuild 的日志:

starting build "e3a0e735-50fc-4315-bafd-03128156d69f"

FETCHSOURCE
Initialized empty Git repository in /workspace/.git/
From https://source.developers.google.com/p/myproject/r/myrepo
* branch 1b67729b8498c35fc19a45b14b8d674635300594 -> FETCH_HEAD
HEAD is now at 1b67729 PrayingforCommit
BUILD
Already have image (with digest): gcr.io/cloud-builders/gcloud
Deploying function (may take a while - up to 2 minutes)...
...............................................done.
availableMemoryMb: 256
entryPoint: process_gcs
eventTrigger:
eventType: google.storage.object.finalize
failurePolicy: {}
resource: projects/_/buckets/mybucket
service: storage.googleapis.com
ingressSettings: ALLOW_ALL
labels:
deployment-tool: cli-gcloud
name: projects/myproject/locations/europe-west1/functions/myfunction
runtime: python37
serviceAccountEmail: myproject@appspot.gserviceaccount.com
sourceRepository:
deployedUrl: https://source.developers.google.com/projects/myproject/repos/myrepo/revisions/2ed14c3225e7fcc089f2bc6a0ae29c7564ec12b9/paths/
url: https://source.developers.google.com/projects/myproject/repos/myrepo/moveable-aliases/master/paths/
status: ACTIVE
timeout: 60s
updateTime: '2020-04-15T00:24:55.184Z'
versionId: '2'
PUSH
DONE

如您所见,触发的提交是 1b67729,但 DeployedUrl 行表示 2ed14c3,这是前一次提交

操作在 2:24:55 结束,我在我的云函数源选项卡中看到了同一时间

enter image description here

如果我只是单击编辑按钮,然后部署按钮,手动强制重建云功能,它会部署正确的提交 (1b67729)

enter image description here

以下是 cloud-function 的参数:

enter image description here

我对 cloudbuild 的错误在哪里,以及如何始终部署最后一次提交???

谢谢你的帮助

最佳答案

我遇到了同样的问题(尽管我使用的是 GitHub 镜像而不是 native Cloud Source Repositories)。
省略 --source 标志时,Cloud Functions 不会检查源存储库的更新
您的函数之前是直接从源存储库部署的,并且您没有传递 --source标记到 gcloud。在这些情况下,gcloud 会忽略本地目录中的代码。
最简单的解决方法是在 cloudbuild.yaml 中明确指定源:

steps:
- name: "gcr.io/cloud-builders/gcloud"
args: ["functions", "deploy", "myfunction", "--region=europe-west1", "--source=."]
timeout: "1600s"
如果该函数从未被配置为直接从存储库中获取其源代码,您就不会遇到此问题。
您的 Cloud Function 之前已从源存储库部署
配置 Cloud Functions 时,可以选择从 Cloud Source Repository 获取源代码。通过控制台创建函数时,系统会提示您将此作为选项,但也可以使用 gcloud:
gcloud functions deploy NAME \
--source https://source.developers.google.com/projects/PROJECT_ID/repos/REPOSITORY_ID/moveable-aliases/master/paths/SOURCE \
[... other gcloud options ...]
这是您可以在构建输出中看到的“sourceRepository”设置。
但是,正如您在控制台 ( resolved to 1b67729b ) 中看到的那样,Cloud Functions 不会从那里更新其代码。
省略 gcloud 的 --source 标志会导致潜在的混淆行为
当您忽略 --source 时标记到 gcloud,如果该函数之前直接从存储库部署,则适用特定行为。来自 gcloud functions deploy 的文档:

If you do not specify the --source flag:

  • The current directory will be used for new function deployments.
  • If the function was previously deployed using a local filesystem path, then the function's source code will be updated using thecurrent directory.
  • If the function was previously deployed using a Google Cloud Storage location or a source repository, then the function's sourcecode will not be updated.

你正在打第三个选项。
通过 Cloud Build 进行部署时,最好不要将 Cloud Functions 直接链接到源存储库
Cloud Build 配置为在包含源代码副本的目录中运行 gcloud。因此,您可以直接从本地文件系统进行部署 - 这会将函数打包到一个 zip 文件中,将其上传到 Cloud Storage,然后告诉 Cloud Functions 获取它。
如果您告诉 Cloud Functions 从存储库中获取其代码,则会对存储库进行第二次 check out 并从那里创建一个 zip 文件,这会稍微慢一些(如果分支在背景)。

关于google-cloud-functions - Google Cloud build 总是使用先前的提交来部署我的云功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61219791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com