首页 > 解决方案 > 如何使用源代码配置 ECS(蓝/绿)并为 taskdef 和 appspec 文件正确构建工件?

问题描述

我知道与此相关的问题已在 aws 文档和此处得到解答。它没有用,有点混乱。请不要将其标记为重复。

我的 ECS 集群、ALB、TaskDefinition、Service 已经到位。我有 3 个阶段的 AWS 代码管道。源 (CodeCommit)、构建 (CodeBuild) 和部署 (ECS 蓝色/绿色)。

直到构建阶段工作正常。我的 docker 镜像被推送到 ECR。我在 CodeCommit 的根目录中推送了 buildspec.yaml、taskdef、json 和 appspec.yaml。

构建规范.yaml

version: 0.2
env:
  variables:
    ECR_REPO: 1234567890.dkr.ecr.us-east-1.amazonaws.com/abc
phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin ${ECR_REPO}
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...          
      - docker build -t abc .
      - echo "Tagging Docker image for ECR.."
      - docker tag abc ${ECR_REPO}:latest      
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker image...      - 
      - docker push ${ECR_REPO}:latest
      - echo Writing image definitions file...
      - printf '[{"imageUri":"%s"}]' ${ECR_REPO}:latest > imageDetail.json
artifacts:
  files:
      - imageDetail.json

任务定义.json

{
   "containerDefinitions": [ 
      { 
         "essential": true,
         "image": "<IMAGE1_NAME>",
         "name": "Staging-Container",
         "portMappings": [ 
            { 
               "containerPort": 80,
               "hostPort": 80,
               "protocol": "tcp"
            }
         ]
      }
   ],
   "cpu": "1024",
   "executionRoleArn": "arn:aws:iam::1234567890:role/ecsTaskExecutionRole",
   "family": "Staging-Task",
   "memory": "1024",
   "networkMode": "bridge",
   "requiresCompatibilities": [ 
       "EC2" 
    ]
}

应用规范.yaml

version: 0.0
Resources:
 - TargetService:
     Type: AWS::ECS::Service
     Properties:
       TaskDefinition: <TASK_DEFINITION>
       LoadBalancerInfo:
         ContainerName: "Staging-Container"
         ContainerPort: 80

在我的 CodePipeline 配置中是:

Build Stage:

Input artifacts: SourceArtifact
Output artifacts: BuildArtifact

Deploy Stage (ECS Blue/Grees):

Input artifacts: BuildArtifact
Amazon ECS task definition : BuildArtifact taskdef.json (in file name)
AWS CodeDeploy AppSpec file: BuildArtifact appspec.yaml (in file name)

Dynamically update task definition image - optional
Input artifact with image details: BuildArtifact
Placeholder text in the task definition: IMAGE1_NAME

在 CodePipeline S3 存储桶中有两个文件夹

BuildArtif (It contains only imageDetail.json)
SourceArti (Source code, taskdef,json, appspec.yaml)

当我运行管道时,它在部署阶段失败并出现以下错误:

Image URI container named: <<IMAGE1_NAME>> does not match any of the missing containers in the task definition file provided.

我已经尝试过本教程和解决方案,但没有帮助:

https://medium.com/@shashank070/in-my-previous-blog-i-have-explained-how-to-do-initial-checks-like-code-review-code-build-cddcc21afd9f

https://stackoverflow.com/questions/62022787/aws-ecs-blue-green-codepipeline-exception-while-trying-to-read-the-image-artifa/62037921#62037921

标签: amazon-web-servicesamazon-ecsaws-codepipelineaws-code-deploy

解决方案


推荐阅读