【问题标题】:Triggering a Google Container Builder build via Cloud Functions通过 Cloud Functions 触发 Google Container Builder 构建
【发布时间】:2018-05-08 20:32:14
【问题描述】:

我正在使用 Container Builder 来处理巨大的 JSON 文件并对其进行转换。如here 所述,很有可能以非标准方式使用它。

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

目前我正在尝试使用 REST API 来触发它(我是 Node.js 的新手),但我的 URL 上出现了 404。我正在一个具有完整 API 访问权限的 Cloud Shell 实例上进行开发。

我试图通过PUT 请求和包含成功运行cloudbuild.yaml 的JSON 等效项的JSON 正文触发的URL 是: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-cloud-functions google-container-builder


    【解决方案1】:

    显然,已经有人这样做了。该库位于 GitHub 上:https://github.com/mhr3/gcp-container-builder-node 并可通过 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);
    });
    

    【讨论】:

    • 我认为这只是创建的构建而不是触发它们。
    • 它确实会创建一个构建并使其立即执行。
    【解决方案2】:

    您提出的程序需要三个不同的步骤:

    谷歌云存储 → 云函数 → API 调用。

    根据您公开的要求,最好使用Container Builder’s Build Triggers

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

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

    【讨论】:

    • 您的意思是我应该滥用 Cloud Source Repositories 并在其中存储巨大的以换行符分隔的 JSON blob?这与 git 的设计方式不矛盾吗?另外我如何标记我已经处理过的文件? (在 GCS 中,很容易将标签添加到 blob 的元数据中。)
    • 你可以使用git tags
    猜你喜欢
    • 1970-01-01
    • 2021-02-04
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    相关资源
    最近更新 更多