簡體   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