简体   繁体   中英

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.

I have 2 github repos: app-cdk and app-website .

I have setup a CodePipeline as follows:

      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

    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. 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?

I have encountered similar scenario, where I had to create a CDK Pipeline for multiple Static S3 sites in a repository.

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.

Whereas my Static S3 Websites was a construct (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

The above Codebuild Project will need a buildspecfile in your root of the repo with the following code (for reference)

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

#!/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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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