简体   繁体   English

如何使用AWS CodePipeline更新ECS上的容器服务

[英]How can I use AWS CodePipeline to update a container service on ECS

I have a cluster on ECS with a container service. 我在ECS上有一个集群服务集群。 I've setup CodePipeline to build a new container on an update and push this to ECR. 我已经设置了CodePipeline来在更新上构建一个新容器并将其推送到ECR。 How can I trigger an update to my cluster to use the newly updated container? 如何触发对群集的更新以使用新更新的容器?

AWS CodePipeline supports to deploy to ECS directly now. AWS CodePipeline现在支持直接部署到ECS。 You can use the new ECS deploy action to update your ECS service to use the new container image you created. 您可以使用新的ECS部署操作来更新ECS服务以使用您创建的新容器映像。 You need to modify your build step to output a configuration file which contains the image URL of the new image you built. 您需要修改构建步骤以输出包含您构建的新映像的映像URL的配置文件。 More details can be found here https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-cd-pipeline.html 更多细节可以在这里找到https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-cd-pipeline.html

I thought i would add my solution with bitbucket-pipelines, which i know technically does not directly answer the question BUT the commands in the bitbucket pipelines steps are practically identical to the buildspec.yml (check docs though for buildspec.yml) for Code Pipeline. 我想我会用bitbucket-pipelines添加我的解决方案,我知道技术上并没有直接回答问题但是bitbucket管道步骤中的命令实际上与代码管道的buildspec.yml(对于buildspec.yml检查docs)相同。

I feel i should add that aws have done a great Job with the GUI in aws control panel for codepiplines it is really straight forward now days and handles deployment and task definition creation for you. 我觉得我应该补充一点,aws已经在aws控制面板中为GUI做了很好的工作,这对于codepiplines来说它现在非常简单,并为您处理部署和任务定义创建。

People looking to integrate tools like JIRA and its deployment features with JIRA tasks, would need to go with bitbucket pipelines! 希望将JIRA及其部署功能与JIRA任务集成在一起的人们需要使用bitbucket管道!

options:
  docker: true
  size: 2x

pipelines:
  tags:
    build-*:
      - step: 
          name: build app
          image: node:10.15.0
          script:
            - npm i
            - npm run build
          artifacts:
            - node_modules/** 
            - dist/**



     - step:
       name: push to aws staging
       # integrate with JIRA <3
       deployment: staging
       image: atlassian/pipelines-awscli:latest
       services:
         - docker
       script:
         # command line JSON processor plugin (important)
         - apk add jq          

         # standard docker login and build
         - eval $(aws ecr get-login --no-include-email)
         - docker build --build-arg notProduction=true -t app:staging .
         - docker tag app:staging ${secretUrl}.amazonaws.com/app:staging
         - docker push ${secretUrl}.amazonaws.com/app:staging

         # gets the latest task definition
         - export TASK_DEF=$(aws ecs describe-task-definition --task-definition "app-staging")

         # gets the specific containerDefinitions array and exports to a json format which is needed for the register-task-definition function
         - export CONTAINER_DEFS=$(echo $TASK_DEF | jq '.taskDefinition.containerDefinitions' | awk -v ORS= -v OFS= '{$1=$1}1')

         # creates a new task definition from the previous definition details
         - aws ecs register-task-definition --family "app-staging" --container-definitions $CONTAINER_DEFS

         # updates ecs
         - aws ecs update-service --cluster "toolkit" --service "app-staging" --task-definition "app-staging"
       artifacts:
         - node_modules/**
         - dist/**

Since you are using CodePipeline you may trigger an CloudFromation stack after your new image is built. 由于您使用的是CodePipeline,因此在构建新映像后可能会触发CloudFromation堆栈。 Then CloudFormation stack will create a new task definition and update your ECS service. 然后,CloudFormation堆栈将创建新的任务定义并更新您的ECS服务。 Here is a reference architecture. 这是一个参考架构。

在此输入图像描述

You can use this reference architecture for Continuous Deployment. 您可以将此参考体系结构用于持续部署。 CloudFormation templates are attached in the above Github repo. CloudFormation模板附加在上面的Github仓库中。

If you're ok with updating containers to new :latest image, stopping ECS cluster's tasks before deploy works just fine. 如果您可以将容器更新为新的:最新映像,那么在部署之前停止ECS集群的任务就可以了。

In buildspec.yml of AWS CodeBuild stage add buildspec.yml AWS CodeBuild阶段添加的

aws ecs list-tasks --cluster "ECS_CLUSTER_NAME" --output text | awk '{print $2}' | while read line ; do aws ecs stop-task --task "$line" --cluster "ECS_CLUSTER_NAME" ; done

with the name of your ECS Cluster instead of ECS_CLUSTER_NAME, to post_build commands section just after your docker push... commands. 在您的docker docker push...命令之后的post_build commands部分,使用ECS群集的名称而不是ECS_CLUSTER_NAME。

Containers will be stopped. 容器将被停止。 And ECS will fulfill desired tasks amount with your newly created docker image. ECS将使用您新创建的泊坞窗图像来完成所需的任务。

PS Don't forget to check IAM so your CodeBuild user role has permissions to perform ecs:* commands. PS不要忘记检查IAM,以便您的CodeBuild用户角色有权执行ecs:*命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM