简体   繁体   English

AWS CDK CodePipeline 部署应用程序和 CDK

[英]AWS CDK CodePipeline deploying app and CDK

I'm using the AWS CDK with typescript and I'd like to automate my CDK and Code Package deployments.我将 AWS CDK 与 typescript 一起使用,我想自动化我的 CDK 和代码 Package 部署。

I have 2 github repos: app-cdk and app-website .我有 2 个 github 存储库: app-cdkapp-website

I have setup a CodePipeline as follows:我已经设置了一个 CodePipeline,如下所示:

      const pipeline = new CodePipeline(this, 'MyAppPipeline', {
      pipelineName: 'MyAppPipeline',
      
      synth: new ShellStep('Synth', {
        input: CodePipelineSource.gitHub(`${ORG_NAME}/app-cdk`, BRANCH_NAME, {
        }),
        commands: ['npm ci', 'npm run build', 'npx cdk synth']
      })
    });

and added a beta stage as follows并添加了一个 beta 阶段,如下所示

    pipeline.addStage(new MyAppStage(this, 'Beta', {
      env: {account: 'XXXXXXXXX', region: 'us-east-2' }
    }))

This works fine when I push code to my CDK code package, and deploys new resources.当我将代码推送到我的 CDK 代码 package 并部署新资源时,这工作正常。 How can I add my website repo as a source to kickoff this pipeline, build in a different manner, and deploy the assets to the necessary resources?如何将我的网站存储库添加为启动此管道的源,以不同的方式构建,并将资产部署到必要的资源? Shouldn't that be a part of the CodePipeline's source and build stages?那不应该是 CodePipeline 的源代码和构建阶段的一部分吗?

I have encountered similar scenario, where I had to create a CDK Pipeline for multiple Static S3 sites in a repository.我遇到过类似的情况,我必须为存储库中的多个 Static S3 站点创建 CDK 管道。

Soon, It became evident, that this had to be done using two stacks as Pipeline requires step to be of type Stage and does not support Construct.很快,很明显,这必须使用两个堆栈来完成,因为 Pipeline 要求 step 的类型为 Stage 并且不支持 Construct。

Whereas my Static S3 Websites was a construct (BucketDeployment).而我的 Static S3 网站是一个构造(BucketDeployment)。

The way in which I handled this integration is as follows我处理这种集成的方式如下

deployment_code_build = cb.Project(self, 'PartnerS3deployment',
                                                   project_name='PartnerStaticS3deployment',
                                                   source=cb.Source.git_hub(owner='<github-org>',
                                                                            repo='<repo-name>', clone_depth=1,
                                                                            webhook_filters=[
                                                                                cb.FilterGroup.in_event_of(
                                                                                    cb.EventAction.PUSH).and_branch_is(
                                                                                    branch_name="main")]),
                                                   environment=cb.BuildEnvironment(
                                                       build_image=cb.LinuxBuildImage.STANDARD_5_0
                                                       ))

This added/provisioned a Codebuild Project which would dynamically deploy the changesets of cdk ls这添加/提供了一个 Codebuild 项目,它将动态部署 cdk ls 的变更集

The above Codebuild Project will need a buildspecfile in your root of the repo with the following code (for reference)上面的 Codebuild 项目将需要一个 buildspecfile 在你的 repo 的根目录中,并带有以下代码(供参考)

version: 0.2
phases:
  install:
    commands:
      - echo Entered in install phase...
      - npm install -g aws-cdk
      - cdk --version
  build:
    commands:
      - pwd
      - cd cdk_pipeline_static_websites
      - ls -lah 
      - python -m pip install -r requirements.txt
      - nohup ./parallel_deploy.sh & echo $! > pidfile && wait $(cat pidfile)
    finally:
      -  echo Build completed on `date`

The contents of parallel_deploy.sh are as follows parallel_deploy.sh的内容如下

#!/bin/bash
for stack in $(cdk list); 
do 
    cdk deploy $stack --require-approval=never &
done;

While this works great, There has to be a simpler alternative which can directly import other stacks/constructs in the CDK Pipeline class.虽然这很好用,但必须有一个更简单的替代方案,可以直接在 CDK 管道 class 中导入其他堆栈/结构。

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

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