繁体   English   中英

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

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

我在ECS上有一个集群服务集群。 我已经设置了CodePipeline来在更新上构建一个新容器并将其推送到ECR。 如何触发对群集的更新以使用新更新的容器?

AWS CodePipeline现在支持直接部署到ECS。 您可以使用新的ECS部署操作来更新ECS服务以使用您创建的新容器映像。 您需要修改构建步骤以输出包含您构建的新映像的映像URL的配置文件。 更多细节可以在这里找到https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-cd-pipeline.html

我想我会用bitbucket-pipelines添加我的解决方案,我知道技术上并没有直接回答问题但是bitbucket管道步骤中的命令实际上与代码管道的buildspec.yml(对于buildspec.yml检查docs)相同。

我觉得我应该补充一点,aws已经在aws控制面板中为GUI做了很好的工作,这对于codepiplines来说它现在非常简单,并为您处理部署和任务定义创建。

希望将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/**

由于您使用的是CodePipeline,因此在构建新映像后可能会触发CloudFromation堆栈。 然后,CloudFormation堆栈将创建新的任务定义并更新您的ECS服务。 这是一个参考架构。

在此输入图像描述

您可以将此参考体系结构用于持续部署。 CloudFormation模板附加在上面的Github仓库中。

如果您可以将容器更新为新的:最新映像,那么在部署之前停止ECS集群的任务就可以了。

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

在您的docker docker push...命令之后的post_build commands部分,使用ECS群集的名称而不是ECS_CLUSTER_NAME。

容器将被停止。 ECS将使用您新创建的泊坞窗图像来完成所需的任务。

PS不要忘记检查IAM,以便您的CodeBuild用户角色有权执行ecs:*命令。

暂无
暂无

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

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