Jon Duffy
Jon Duffy

Reputation: 81

CDK pipelines branching strategy

CDK pipelines seems to only work, by default, with one branch. Am I missing something or is there a way to:

Ideally we do not want to have to push everything to the master branch to deploy to dev / test, so that we can keep the master branch clean, tidy, and stable.

I have thought about having multiple pipelines, one for dev, one for test, and one for master, this would solve the issue, but doesn’t feel like the cleanest solution.

Are there any recommended patterns?

Upvotes: 5

Views: 1759

Answers (3)

Jon Duffy
Jon Duffy

Reputation: 81

Solution

Building on what @gshpychka said https://stackoverflow.com/a/69812428/12907894

A pipeline that deploys pipleines. I found lots of overcomplicated solutions online, but in the end it turned out to be quite simple.

Just adding extra pipelines, for each branch we wished to deploy.

A core pipeline that builds the branch pipelines.

Only variables that need to change between any of this:

  • Account ID
  • env name
  • branch name

Account Pattern

  • Build
  • Dev
  • Staging
  • Prod

core-pipeline

branch

master

  • Webhook -> null (so it doesn't fire on each build)

Deploys:

  • master-pipeline -> build account
  • staging-pipeline -> build account

master-pipeline

branch

master

deploys

  • app stack -> prod account

staging-pipeline

branch

Staging

deploys

  • app stack -> staging account

Upvotes: 1

lynkfox
lynkfox

Reputation: 2400

Codepipeline cannot branch. It is not designed to do so.

A solution is to have a multi stage pipeline that has manual approval steps in the middle if you absolutely must have multiple environments and a single pipeline.

That is

Source (Dev branch) -> Build/Deploy -> Manual Approval step -> Make use of of a Codebuild or a lambda to move your now tested code (still in the artifact chain) to your test branch for you (ie make use of a git server api to initiate the merge based on the commit message from the initial commit that started the chain -> Another Build./Deploy to your test env (can even do cross account deployment here) -> Manual Approval step -> Repeat as many times as you want until you deploy to Production.

However.... this is entirely a hack. You're better off with multiple pipelines. I would use the CDK to be able to dynamically adjust the cloudformation template for the pipeline itself to handle Dev/Prod and then simply deploy it twice, linking one to the source of Dev and one to the source of Main.

Upvotes: 0

gshpychka
gshpychka

Reputation: 11587

The AWS-prescribed best practice is to use trunk-based development.

Thus, a single pipeline cannot use multiple branches for deploying to different environments cleanly.

You should look into creating a single pipeline that would in turn create environment-specific pipelines.

Here is a relevant issue in the CDK repo: https://github.com/aws/aws-cdk/issues/9461

Upvotes: 3

Related Questions