通过 Cloud Functions 触发 Google Container Builder 构建

Triggering a Google Container Builder build via Cloud Functions

我正在使用 Container Builder 处理巨大的 JSON 文件并 运行 形成它们。如 here.

所述,很可能会以非标准方式使用它

是否可以触发容器构建器构建并通过云函数向其传递参数?这将允许对 GCS 中新上传的文件进行操作,并通过容器构建器自动处理它们。

目前我正在尝试使用 REST API 来触发它(我是 Node.js 的新手),但我的 URL 收到 404。我正在具有完全 API 访问权限的云 Shell 实例上进行开发。

我试图通过 PUT 请求触发的 URL 和包含 JSON 等同于成功 运行 [的 JSON 正文=13=] 是:https://cloudbuild.googleapis.com/v1/projects/[PROJECT_ID]/builds

我正在使用来自 Node.js 的请求库:

request({ url: "https://cloudbuild.googleapis.com/v1/projects/[PROJECT_ID]/builds", 
    method: 'PUT', 
    json: {"steps":[{"name":"gcr.io/cloud-builders/gsutil",(...),
    function(error, response, body){
        console.log(error)
        console.log(response)
        console.log(body)
    })

您建议的程序需要三个不同的步骤:

Google 云存储 → 云函数 → API 调用。

根据你暴露的要求,使用Container Builder’s Build Triggers会更好。

您将文件上传到 Google Cloud Source Repository 并创建触发器。每次您将更改上传到存储库时,Container Builder 都会自动构建映像。这样您就可以避免使用云函数、API 调用和 Node.js。

这会将程序减少到只有一个步骤,从而降低了复杂性并提高了可靠性。

看来,已经有人做到了。图书馆在 GitHub:https://github.com/mhr3/gcp-container-builder-node and available via npm: https://www.npmjs.com/package/gcp-container-builder

起初我不清楚用法,但现在我是这样使用的:

var build = Object.create(null);
build.steps = [{
    name: 'gcr.io/cloud-builders/gsutil',
    args: ['cp', 'gs://some_bucket/some_file.json', '/workspace/some_file.json']
}]

// more build steps, converting the file, uploading it, etc.

builder.createBuild(build, function(err, resp) {
    console.log(err);
    console.log(resp);
});