Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pipelines): Add feature to display all changesets for stage before manual approval step + added postPrepare Step #25413 #28408

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
// Has convenient settings for attaching to a NodeJS process for debugging purposes
// that are NOT the default and otherwise every developers has to configure for
Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,139 @@
// eslint-disable-next-line import/no-extraneous-dependencies
/// !cdk-integ PipelineStack pragma:set-context:@aws-cdk/core:newStyleStackSynthesis=true
import * as sqs from 'aws-cdk-lib/aws-sqs';
import { App, Stack, StackProps, Stage, StageProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { App, Fn, Stack, StackProps, Stage, StageProps } from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as pipelines from 'aws-cdk-lib/pipelines';

import { Construct } from 'constructs';
class PipelineStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);

const vpc = ec2.Vpc.fromVpcAttributes(this, 'vpc', {
availabilityZones: ['eu-central-1a', 'eu-central-1b', 'eu-central-1c'],
vpcId: Fn.importValue('VPC1-VPC-ID'),
privateSubnetIds: [
Fn.importValue('VPC1-AZ1Subnet1'),
Fn.importValue('VPC1-AZ2Subnet1'),
Fn.importValue('VPC1-AZ3Subnet1'),
],
privateSubnetRouteTableIds: [
Fn.importValue('VPC1-RouteTableIDAZ1'),
Fn.importValue('VPC1-RouteTableIDAZ2'),
Fn.importValue('VPC1-RouteTableIDAZ3'),
],
});

const pipeline = new pipelines.CodePipeline(this, 'Pipeline', {
synth: new pipelines.ShellStep('Synth', {
input: pipelines.CodePipelineSource.gitHub('rix0rrr/cdk-pipelines-demo', 'main'),
commands: [
'npm ci',
'npm run build',
'npx cdk synth',
],
input: pipelines.CodePipelineSource.s3(
s3.Bucket.fromBucketName(
this,
'SourceBucket-' + id,
'290582178775-gitsync',
),
'mobility-operations-experience/serviceteam/services/test-cdk-contribution/main/src/' +
'mobility-operations-experience_serviceteam_services_test-cdk-contribution.zip',
),
commands: ['npm ci', 'npm run build', 'npx cdk synth'],
}),
pipelineName: 'test-cdk-contribution',
selfMutation: false,
synthCodeBuildDefaults: {
vpc: vpc,
},
allPrepareNodesFirst: true,
});

pipeline.addStage(new AppStage(this, 'Beta'));
// const beta = new AppStage(this, 'Beta');
// pipeline.addStage(beta, {
// allPrepareNodesFirst: true,
// stackSteps: [
// {
// stack: beta.stack1,
// changeSet: [new pipelines.ManualApprovalStep('b approve')], // Executed after stack is prepared but before the stack is deployed
// },
// ],
// });
// const st=pipeline.addStage(new AppStage(this, 'test'), {
// allPrepareNodesFirst: true,
// postPrepare: [new pipelines.ManualApprovalStep('Approval0')],
// });
// console.log(st.postPrepare);
const group = pipeline
.addWave('Wave1', {
postPrepare: [new pipelines.ManualApprovalStep('Approval1')],
});
// group.addPostPrepare(new pipelines.ManualApprovalStep('Approval11'));

const group = pipeline.addWave('Wave1');
group.addStage(new AppStage(this, 'Prod1'));
// group.addStage(new AppStage2(this, 'Prod1'), {
// // postPrepare: [new pipelines.ManualApprovalStep('Approval13')],
// });
group.addStage(new AppStage(this, 'Prod2'));

const group2 = pipeline.addWave('Wave2');
const group2 = pipeline.addWave('Wave2', {
// postPrepare: [new pipelines.ManualApprovalStep('Approval2')],
});
group2.addStage(new AppStage(this, 'Prod3'));
group2.addStage(new AppStage(this, 'Prod4'));
group2.addStage(new AppStage(this, 'Prod5'));
group2.addStage(new AppStage(this, 'Prod6'));
// group2.addStage(new AppStage(this, 'Prod4'));
// group2.addStage(new AppStage(this, 'Prod5'));
// group2.addStage(new AppStage(this, 'Prod6'));
}

}


class AppStage extends Stage {
public readonly stack1: Stack;
public readonly stack2: Stack;
constructor(scope: Construct, id: string, props?: StageProps) {
super(scope, id, props);

const stack1 = new Stack(this, 'Stack1');
const queue1 = new sqs.Queue(stack1, 'Queue');
this.stack1 = new Stack(this, 'Stack1');

const stack2 = new Stack(this, 'Stack2');
new sqs.Queue(stack2, 'OtherQueue', {
deadLetterQueue: {
queue: queue1,
maxReceiveCount: 5,
},
});
// const q1=new sqs.Queue(this.stack1, 'Queue');
this.stack2 = new Stack(this, 'Stack2');
this.stack2.addDependency(this.stack1);
// new sqs.Queue(this.stack2, 'OtherQueue', { deadLetterQueue: { queue: q1, maxReceiveCount: 1 } });
}

}

// class AppStage3 extends Stage {
// public readonly stack1: Stack;

// constructor(scope: Construct, id: string, props?: StageProps) {
// super(scope, id, props);

// this.stack1 = new Stack(this, 'Stack1');


// // new sqs.Queue(this.stack2, 'OtherQueue', { deadLetterQueue: { queue: q1, maxReceiveCount: 1 } });
// }
// }
// class AppStage4 extends Stage {

// public readonly stack2: Stack;
// constructor(scope: Construct, id: string, props?: StageProps) {
// super(scope, id, props);

// // const q1=new sqs.Queue(this.stack1, 'Queue');
// this.stack2 = new Stack(this, 'Stack2');
// // new sqs.Queue(this.stack2, 'OtherQueue', { deadLetterQueue: { queue: q1, maxReceiveCount: 1 } });
// }
// }
// class AppStage2 extends Stage {
// public readonly stack3: Stack;
// constructor(scope: Construct, id: string, props?: StageProps) {
// super(scope, id, props);
// this.stack3 = new Stack(this, 'Stack3');
// }
// }

const app = new App({
context: {
'@aws-cdk/core:newStyleStackSynthesis': '1',
},
});
new PipelineStack(app, 'PipelineStack');
new PipelineStack(app, 'TestCdkContributionStack');
app.synth();
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// eslint-disable-next-line import/no-extraneous-dependencies
/// !cdk-integ PipelineStack pragma:set-context:@aws-cdk/core:newStyleStackSynthesis=true
import { App, Stack, StackProps, Stage, StageProps } from 'aws-cdk-lib';
import * as sqs from 'aws-cdk-lib/aws-sqs';
import * as pipelines from 'aws-cdk-lib/pipelines';
import { Construct } from 'constructs';

class PipelineStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);

const pipeline = new pipelines.CodePipeline(this, 'Pipeline', {
synth: new pipelines.ShellStep('Synth', {
input: pipelines.CodePipelineSource.gitHub(
'rix0rrr/cdk-pipelines-demo',
'main',
),
commands: ['npm ci', 'npm run build', 'npx cdk synth'],
}),
allPrepareNodesFirst: true,
});

pipeline.addStage(new AppStage(this, 'Beta'), {
});

const group = pipeline.addWave('Wave1');
group.addStage(new AppStage(this, 'Prod1'));
group.addStage(new AppStage(this, 'Prod2'));

const group2 = pipeline.addWave('Wave2');
group2.addStage(new AppStage2(this, 'Prod3'));
group2.addStage(new AppStage3(this, 'Prod4'));
}
}

class AppStage extends Stage {
constructor(scope: Construct, id: string, props?: StageProps) {
super(scope, id, props);

const stack1 = new Stack(this, 'Stack1');
const queue1 = new sqs.Queue(stack1, 'Queue');

const stack2 = new Stack(this, 'Stack2');
new sqs.Queue(stack2, 'OtherQueue', {
deadLetterQueue: {
queue: queue1,
maxReceiveCount: 5,
},
});
}
}

class AppStage2 extends Stage {
constructor(scope: Construct, id: string, props?: StageProps) {
super(scope, id, props);

new Stack(this, 'Stack1');
}
}

class AppStage3 extends Stage {
constructor(scope: Construct, id: string, props?: StageProps) {
super(scope, id, props);

new Stack(this, 'Stack2');
}
}

const app = new App({
context: {
'@aws-cdk/core:newStyleStackSynthesis': '1',
},
});
new PipelineStack(app, 'PipelineWithAllPrepareNodesFirstStack');
app.synth();
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// eslint-disable-next-line import/no-extraneous-dependencies
/// !cdk-integ PipelineStack pragma:set-context:@aws-cdk/core:newStyleStackSynthesis=true
import { App, Stack, StackProps, Stage, StageProps } from 'aws-cdk-lib';
import * as sqs from 'aws-cdk-lib/aws-sqs';
import * as pipelines from 'aws-cdk-lib/pipelines';
import { Construct } from 'constructs';

class PipelineStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);

const pipeline = new pipelines.CodePipeline(this, 'PipelineWithPostPrepare', {
synth: new pipelines.ShellStep('Synth', {
input: pipelines.CodePipelineSource.gitHub(
'rix0rrr/cdk-pipelines-demo',
'main',
),
commands: ['npm ci', 'npm run build', 'npx cdk synth'],
}),
allPrepareNodesFirst: true,
});

pipeline.addStage(new AppStage(this, 'Beta'), {
postPrepare: [new pipelines.ManualApprovalStep('Approval0')],
});

const group = pipeline.addWave('Wave1', {

postPrepare: [new pipelines.ManualApprovalStep('Approval1')],
});
group.addStage(new AppStage(this, 'Prod1'));
group.addStage(new AppStage(this, 'Prod2'));

const group2 = pipeline.addWave('Wave2', { postPrepare: [new pipelines.ManualApprovalStep('Approval2')] });
group2.addStage(new AppStage2(this, 'Prod3'));
group2.addStage(new AppStage3(this, 'Prod4'));
}
}

class AppStage extends Stage {
constructor(scope: Construct, id: string, props?: StageProps) {
super(scope, id, props);

const stack1 = new Stack(this, 'Stack1');
const queue1 = new sqs.Queue(stack1, 'Queue');

const stack2 = new Stack(this, 'Stack2');
new sqs.Queue(stack2, 'OtherQueue', {
deadLetterQueue: {
queue: queue1,
maxReceiveCount: 5,
},
});
}
}

class AppStage2 extends Stage {
constructor(scope: Construct, id: string, props?: StageProps) {
super(scope, id, props);

new Stack(this, 'Stack1');
}
}

class AppStage3 extends Stage {
constructor(scope: Construct, id: string, props?: StageProps) {
super(scope, id, props);

new Stack(this, 'Stack2');
}
}

const app = new App({
context: {
'@aws-cdk/core:newStyleStackSynthesis': '1',
},
});
new PipelineStack(app, 'PipelineWithPostPrepareStack');
app.synth();
41 changes: 36 additions & 5 deletions packages/aws-cdk-lib/pipelines/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# CDK Pipelines


A construct library for painless Continuous Delivery of CDK applications.

CDK Pipelines is an *opinionated construct library*. It is purpose-built to
Expand Down Expand Up @@ -162,9 +161,9 @@ has been bootstrapped (see below), and then execute deploying the
Run the following commands to get the pipeline going:

```console
$ git commit -a
$ git push
$ cdk deploy PipelineStack
git commit -a
git push
cdk deploy PipelineStack
```

Administrative permissions to the account are only necessary up until
Expand Down Expand Up @@ -565,6 +564,38 @@ class PipelineStack extends Stack {
}
```

#### Deploying with all change sets at first

Deployment is done by default with `CodePipeline` engine using change sets,
i.e. to first create a change set and then execute it. This allows you to inject
steps that inspect the change set and approve or reject it, but failed deployments
are not retryable and creation of the change set costs time. The change sets tough are
being sorted within the pipeline by its dependencies. This means that some of the change set
might not be at the top level of a stage. Therefore there is the possibility to define, that
every change set is set as the first action (all in parallel)

The creation of change sets at the top level can be switched on by setting `allPrepareNodesFirst: true`.
`useChangeSets` needs to be activated in order to use this feature.

```ts
declare const synth: pipelines.ShellStep;

class PipelineStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);

const pipeline = new pipelines.CodePipeline(this, 'Pipeline', {
synth,

allPrepareNodesFirst: true,
});
}
}
```

It is further possible to add Steps in between the change sets and the deploy nodes (e.g. a manual approval step). This allows inspecting all change sets before deploying the stacks
in the desired order.

### Validation

Every `addStage()` and `addWave()` command takes additional options. As part of these options,
Expand Down Expand Up @@ -1608,7 +1639,7 @@ $ env CDK_NEW_BOOTSTRAP=1 npx cdk bootstrap \
```

- Update all impacted stacks in the pipeline to use this new qualifier.
See https://docs.aws.amazon.com/cdk/latest/guide/bootstrapping.html for more info.
See <https://docs.aws.amazon.com/cdk/latest/guide/bootstrapping.html> for more info.

```ts
new Stack(this, 'MyStack', {
Expand Down
Loading
Loading