将单个 Codebuild 项目部署到多个 ECS 容器
Deploying single Codebuild project to multiple ECS containers
我成功使用 codebuild 和 codepipeline 将多个项目持续部署到 ECS 上,但遇到了问题。
在这个项目中,我需要将相同的构建部署到 四个不同的 ECS 容器。
我使用 codebuild 和 codepipeline CD 的默认方式如 aws 文档中所示 - 我在构建过程结束时创建 imagedefinitions.json 文件。
据我所知,这个文件可以包含 只有一个 ECS 容器的定义 。
您必须提供容器的名称:
post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker images...
- docker push $REPOSITORY_URI:latest
- docker push $REPOSITORY_URI:$IMAGE_TAG
- echo Writing image definitions file...
- printf '[{"name":"hello-world","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
artifacts:
files: imagedefinitions.json
对于此任务定义:
{
"taskDefinition": {
"family": "hello-world",
"containerDefinitions": [
{
"name": "hello-world",
"image": "012345678910.dkr.ecr.us-west-2.amazonaws.com/hello-world:6a57b99",
"cpu": 100,
"portMappings": [
{
"protocol": "tcp",
"containerPort": 80,
"hostPort": 80
}
],
"memory": 128,
"essential": true
}
]
如果我将不同服务中的所有四个容器的名称更改为相同的名称,可能 会起作用。例如那个特定的图像名称。但我不知道这是否是个好主意。
现在我想知道我是否可以在这个项目中使用 codepipeline 到 ECS,或者我应该以不同的方式部署。
codePipeline 中的 CodeBuild 操作目前仅支持一个输出工件。您可以将所有图像定义文件压缩到一个 zip 文件中,并添加一个 lambda 调用操作以将其拆分为多个工件并在 ECS 部署操作中使用它们。
您可以构建文件以在 imagedefinitions.json 文件中列出多个容器映像对,如下所示:
[{
"name": "simple-app",
"imageUri": "httpd:2.4"
},
{
"name": "simple-app",
"imageUri": "mysql"
},
{
"name": "simple-app-2",
"imageUri": "java1.8"
}]
这是在我的 buildspec.yml 文件中如何完成的示例:
post_build:
commands:
- docker push $IMAGE1_URI:$IMAGE_TAG
- docker push $IMAGE2_URI:$IMAGE_TAG
- printf '[{"name":"conatiner1_name","imageUri":"%s"}, {"name":"container2_name","imageUri":"%s"}]' $IMAGE1_URI:$IMAGE_TAG $IMAGE2_URI:$IMAGE_TAG > imagedefinitions.json
我们 运行 CodeBuild 作为 CodePipeline 下的一个动作。
下面是 运行 同一 ECR 映像上的两个不同 ECS 服务任务的示例:
phases:
install: ...
pre_build: ...
build: ...
post_build:
commands:
- ...
- printf '[{"name":"web","imageUri":"%s"}]' $ECR_REPOSITORY_URL:$LATEST_VERSION > web_image_definitions.json
- printf '[{"name":"worker","imageUri":"%s"}]' $ECR_REPOSITORY_URL:$LATEST_VERSION > worker_image_definitions.json
artifacts:
files:
- web_image_definitions.json
- worker_image_definitions.json
现在,在您的 CodePipeline 操作中,您想要将 Output artifacts
命名为构建操作,例如BuildOutput
.
接下来创建两个部署操作(每个 ECS 服务一个)并针对您要确保将 Input artifacts
设置为 BuildOutput
并将 Image definitions file - optional
分别设置为 web_image_definitions.json
和 worker_image_definitions.json
用于每个部署操作。
我成功使用 codebuild 和 codepipeline 将多个项目持续部署到 ECS 上,但遇到了问题。 在这个项目中,我需要将相同的构建部署到 四个不同的 ECS 容器。
我使用 codebuild 和 codepipeline CD 的默认方式如 aws 文档中所示 - 我在构建过程结束时创建 imagedefinitions.json 文件。 据我所知,这个文件可以包含 只有一个 ECS 容器的定义 。
您必须提供容器的名称:
post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker images...
- docker push $REPOSITORY_URI:latest
- docker push $REPOSITORY_URI:$IMAGE_TAG
- echo Writing image definitions file...
- printf '[{"name":"hello-world","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
artifacts:
files: imagedefinitions.json
对于此任务定义:
{
"taskDefinition": {
"family": "hello-world",
"containerDefinitions": [
{
"name": "hello-world",
"image": "012345678910.dkr.ecr.us-west-2.amazonaws.com/hello-world:6a57b99",
"cpu": 100,
"portMappings": [
{
"protocol": "tcp",
"containerPort": 80,
"hostPort": 80
}
],
"memory": 128,
"essential": true
}
]
如果我将不同服务中的所有四个容器的名称更改为相同的名称,可能 会起作用。例如那个特定的图像名称。但我不知道这是否是个好主意。
现在我想知道我是否可以在这个项目中使用 codepipeline 到 ECS,或者我应该以不同的方式部署。
codePipeline 中的 CodeBuild 操作目前仅支持一个输出工件。您可以将所有图像定义文件压缩到一个 zip 文件中,并添加一个 lambda 调用操作以将其拆分为多个工件并在 ECS 部署操作中使用它们。
您可以构建文件以在 imagedefinitions.json 文件中列出多个容器映像对,如下所示:
[{
"name": "simple-app",
"imageUri": "httpd:2.4"
},
{
"name": "simple-app",
"imageUri": "mysql"
},
{
"name": "simple-app-2",
"imageUri": "java1.8"
}]
这是在我的 buildspec.yml 文件中如何完成的示例:
post_build:
commands:
- docker push $IMAGE1_URI:$IMAGE_TAG
- docker push $IMAGE2_URI:$IMAGE_TAG
- printf '[{"name":"conatiner1_name","imageUri":"%s"}, {"name":"container2_name","imageUri":"%s"}]' $IMAGE1_URI:$IMAGE_TAG $IMAGE2_URI:$IMAGE_TAG > imagedefinitions.json
我们 运行 CodeBuild 作为 CodePipeline 下的一个动作。
下面是 运行 同一 ECR 映像上的两个不同 ECS 服务任务的示例:
phases:
install: ...
pre_build: ...
build: ...
post_build:
commands:
- ...
- printf '[{"name":"web","imageUri":"%s"}]' $ECR_REPOSITORY_URL:$LATEST_VERSION > web_image_definitions.json
- printf '[{"name":"worker","imageUri":"%s"}]' $ECR_REPOSITORY_URL:$LATEST_VERSION > worker_image_definitions.json
artifacts:
files:
- web_image_definitions.json
- worker_image_definitions.json
现在,在您的 CodePipeline 操作中,您想要将 Output artifacts
命名为构建操作,例如BuildOutput
.
接下来创建两个部署操作(每个 ECS 服务一个)并针对您要确保将 Input artifacts
设置为 BuildOutput
并将 Image definitions file - optional
分别设置为 web_image_definitions.json
和 worker_image_definitions.json
用于每个部署操作。