是否可以使用 Google CloudBuild 将映像推送到 AWS ECR?

时间:2021-03-28 18:57:17

标签: google-cloud-build amazon-ecr

以下代码段 (Node/Typescript) 利用 Google 的 CloudBuild API (v1) 构建容器并推送到 Google 的容器注册表 (GCR)。如果可能,让 CloudBuild 将映像推送到 AWS ECR 而不是 GCR 的正确方法是什么?

import { cloudbuild_v1 } from "googleapis";

[...]

const manifestLocation = `gs://${manifestFile.bucket}/${manifestFile.fullpath}`;
const buildDestination = `gcr.io/${GOOGLE_PROJECT_ID}/xxx:yyy`;

const result = await builds.create({
    projectId: GOOGLE_PROJECT_ID,
    requestBody: {
        steps: [
            {
                name: 'gcr.io/cloud-builders/gcs-fetcher',
                args: [
                    '--type=Manifest',
                    `--location=${manifestLocation}`
                ]
            },
            {
                name: 'docker',
                args: ['build', '-t', buildDestination, '.'],
            }
        ],
        images: [buildDestination]
    }
})```

1 个答案:

答案 0 :(得分:2)

是的,您可以通过设置自定义步骤来执行此操作。

为此,您可以使用 docker 映像执行构建并将其推送到 AWS ECR。

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'build', '-t', '<AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com/<IMAGE_NAME>', '.' ]

Here 是关于如何使用 cludbuild 的指南,对您很有用。

基本上在您的用例中,您可以像这样将目的地的值更改为 AWS ECR URL:

import { cloudbuild_v1 } from "googleapis";

[...]

const manifestLocation = `gs://${manifestFile.bucket}/${manifestFile.fullpath}`;
const buildDestination = `<AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com/<IMAGE_NAME>`;

const result = await builds.create({
    projectId: GOOGLE_PROJECT_ID,
    requestBody: {
        steps: [
            {
                name: 'gcr.io/cloud-builders/gcs-fetcher',
                args: [
                    '--type=Manifest',
                    `--location=${manifestLocation}`
                ]
            },
            {
                name: 'docker',
                args: ['build', '-t', buildDestination, '.'],
            }
        ],
        images: [buildDestination]
    }
})```
Answer