aws(学习笔记第四十六课) codepipeline-build-deploy

发布于:2025-06-25 ⋅ 阅读:(25) ⋅ 点赞:(0)

文章目录

aws(学习笔记第四十六课) codepipeline-build-deploy

  • 使用codebuild
  • 尝试codedeploy

学习内容:

  • 定义codebuild
  • 使用codedeploy

1. 代码链接及整体架构

1.1 代码链接

代码连接(codepipeline-build-deploy)

1.2 整体架构

整体架构分为三个部分:

  • 初始化阶段,创建codecommit repo以及codebuild project
  • 创建vpcpublic albalb listener以及fargate service
  • 创建sourcebuild以及deploystage以及deplyment group,将部署和alb listener进行关联
1.2.1 初始化阶段的codecommit repo以及codebuild project设定

在这里插入图片描述

这里,trigger code build分为两个阶段:

  • 初期阶段,通过custom resource创建 ==> trigger lambda执行 ==> trigger code build project
  • 平时运用阶段,通过code commitpush ==> trigger code build project
1.2.2 创建vpcpublic albalb listener以及fargate service

这些组件都是真正运行服务的部分。
在这里插入图片描述

  • 首先生成一个Application Loader Balancer
  • 对这个Application Loader Balancer设定listener
  • 之后生成Target Group GreenTarget Group Blue 这里实现蓝绿部署
  • Application Loader Balancer Listener设定defaulttarget group,这里设定为Target Group Green
1.2.3 创建sourcebuild以及deploystage以及deplyment group,将部署和alb listener进行关联

在这里插入图片描述

  • 创建source stageinputcode repo
  • source stage将整个code repo生成source artifact
  • 创建build stageinputsource artifact
  • build stage对整个source artifact进行build,主要是使用buildspec.yaml
  • build stage进行完docker build之后,将生成的docker image上传到ecr
  • build stage进行完docker build之后,生成出deploy stage需要的appspec.yaml && cat taskdef.json
  • deploy stage使用appspec.yaml && cat taskdef.json对整个deployment group进行部署

2. 代码详细分析

2.1 初始化阶段代码,创建codecommit repo以及codebuild project

2.1.1 创建codecommit repo
# Creates an AWS CodeCommit repository
        code_repo = codecommit.Repository(
            self, "CodeRepo",
            repository_name="simple-app-code-repo",
            # Copies files from app directory to the repo as the initial commit
            code=codecommit.Code.from_directory("app", "main")
        )

在这里插入图片描述

2.1.2 创建Elastic Container Registry (ECR) image repository
  # Creates an Elastic Container Registry (ECR) image repository
        image_repo = ecr.Repository(self, "ImageRepo")

在这里插入图片描述

2.1.3 创建fargate task definition
        # Creates a Task Definition for the ECS Fargate service
        fargate_task_def = ecs.FargateTaskDefinition(self, "FargateTaskDef")
        fargate_task_def.add_container(
            "Container",
            container_name="web",
            image=ecs.ContainerImage.from_ecr_repository(image_repo),
            port_mappings=[{
   "containerPort": 80}]
        )

这里,创建一个container(docker),并将端口映射出80
在这里插入图片描述

2.1.4 创建code build project
        # CodeBuild project that builds the Docker image
        build_image = codebuild.Project(
            self, "BuildImage",
            build_spec=codebuild.BuildSpec.from_source_filename(
                "buildspec.yaml"),
            source=codebuild.Source.code_commit(
                repository=code_repo,
                branch_or_ref="main"
            ),
            environment=codebuild.BuildEnvironment(
                privileged=True
            ),
            environment_variables={
   
                "AWS_ACCOUNT_ID": codebuild.BuildEnvironmentVariable(value=os.getenv('CDK_DEFAULT_ACCOUNT') or ""),
                "REGION": codebuild.BuildEnvironmentVariable(value=os.getenv('CDK_DEFAULT_REGION') or ""),
                "IMAGE_TAG": codebuild.BuildEnvironmentVariable(value="latest"),
                "IMAGE_REPO_NAME": codebuild.BuildEnvironmentVariable(value=image_repo.repository_name),
                "REPOSITORY_URI": codebuild.BuildEnvironmentVariable(value=image_repo.repository_uri),
                "TASK_DEFINITION_ARN": codebuild.BuildEnvironmentVariable(value=fargate_task_def.task_definition_arn),
                "TASK_ROLE_ARN": codebuild.BuildEnvironmentVariable(value=fargate_task_def.

网站公告

今日签到

点亮在社区的每一天
去签到