@flit/cdk-pipeline 1.0.9 → 1.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,195 @@
1
+ import { Stack } from "aws-cdk-lib";
2
+ import {
3
+ BuildEnvironmentVariable,
4
+ BuildSpec,
5
+ ComputeType,
6
+ LinuxBuildImage,
7
+ Project,
8
+ } from "aws-cdk-lib/aws-codebuild";
9
+ import { IAction } from "aws-cdk-lib/aws-codepipeline";
10
+ import {
11
+ CloudFormationCreateReplaceChangeSetAction,
12
+ CloudFormationExecuteChangeSetAction,
13
+ CodeBuildAction,
14
+ ManualApprovalAction,
15
+ } from "aws-cdk-lib/aws-codepipeline-actions";
16
+ import * as path from "path";
17
+
18
+ import { PublishAssetsAction } from "./publish-assets-action";
19
+ import { Artifact } from "./artifact";
20
+ import { Segment, SegmentConstructed } from "./segment";
21
+ import { Pipeline } from "./pipeline";
22
+
23
+ export interface StackSegmentProps {
24
+ /**
25
+ * The stack to be deployed in this segment.
26
+ */
27
+ readonly stack: Stack;
28
+ /**
29
+ * The input arfifact for the build stage.
30
+ */
31
+ readonly input: Artifact | Artifact[];
32
+ /**
33
+ * The command(s) to build the stack.
34
+ * @example "cdk synth StackName --strict --exclusively"
35
+ */
36
+ readonly command: string | string[];
37
+ /**
38
+ * The environmental variables for the build stage.
39
+ */
40
+ readonly environmentVariables?: { [key: string]: BuildEnvironmentVariable };
41
+ /**
42
+ * The name of the stack to deploy the changes to.
43
+ * @deafult The name of the given stack.
44
+ */
45
+ readonly stackName?: string;
46
+ /**
47
+ * The AWS account this Action is supposed to operate in.
48
+ */
49
+ readonly account?: string;
50
+ /**
51
+ * The AWS region the given Action resides in.
52
+ */
53
+ readonly region?: string;
54
+ /**
55
+ * The artifact to hold the stack deployment output file.
56
+ * @default no output artifact
57
+ */
58
+ readonly output?: Artifact;
59
+ /**
60
+ * Does this stage require manual approval of the change set?
61
+ * @default false
62
+ */
63
+ readonly manualApproval?: Boolean;
64
+ }
65
+
66
+ /**
67
+ * @category Segments
68
+ */
69
+ export class StackSegment extends Segment {
70
+ readonly props: StackSegmentProps;
71
+
72
+ constructor(props: StackSegmentProps) {
73
+ super(props);
74
+ this.props = props;
75
+ }
76
+
77
+ construct(scope: Pipeline): SegmentConstructed {
78
+ return new StackSegmentConstructed(
79
+ scope,
80
+ `Deploy${this.props.stack.stackName}`,
81
+ {
82
+ ...this.props,
83
+ input: this.inputs[0],
84
+ extraInputs: this.inputs.slice(1),
85
+ }
86
+ );
87
+ }
88
+ }
89
+
90
+ export interface StackSegmentConstructedProps {
91
+ readonly stack: Stack;
92
+ readonly command: string | string[];
93
+ readonly environmentVariables?: { [key: string]: BuildEnvironmentVariable };
94
+ readonly stackName?: string;
95
+ readonly account?: string;
96
+ readonly region?: string;
97
+ readonly input: Artifact;
98
+ readonly extraInputs?: Artifact[];
99
+ readonly output?: Artifact;
100
+ readonly manualApproval?: Boolean;
101
+ }
102
+
103
+ export class StackSegmentConstructed extends SegmentConstructed {
104
+ readonly name: string;
105
+ readonly actions: IAction[];
106
+
107
+ constructor(
108
+ scope: Pipeline,
109
+ id: string,
110
+ props: StackSegmentConstructedProps
111
+ ) {
112
+ super(scope, id);
113
+
114
+ this.name = props.stack.stackName;
115
+
116
+ const buildArtifact = new Artifact();
117
+
118
+ this.actions = [
119
+ new CodeBuildAction({
120
+ actionName: "Build",
121
+ runOrder: 1,
122
+ input: props.input,
123
+ extraInputs: props.extraInputs,
124
+ outputs: [buildArtifact],
125
+ environmentVariables: props.environmentVariables,
126
+ project: new Project(this, "UpdateCodeBuild", {
127
+ environment: {
128
+ computeType: ComputeType.MEDIUM,
129
+ buildImage: LinuxBuildImage.AMAZON_LINUX_2_4,
130
+ privileged: true,
131
+ },
132
+ buildSpec: BuildSpec.fromObject({
133
+ version: "0.2",
134
+ phases: {
135
+ install: {
136
+ "runtime-versions": {
137
+ docker: 20,
138
+ nodejs: 18,
139
+ },
140
+ commands: ["npm ci"],
141
+ },
142
+ build: {
143
+ commands: props.command,
144
+ },
145
+ },
146
+ artifacts: {
147
+ files: [path.join(scope.buildDir, "**/*")],
148
+ },
149
+ cache: {
150
+ paths: ["/root/.npm/**/*"],
151
+ },
152
+ }),
153
+ }),
154
+ }),
155
+ new PublishAssetsAction(this, "PublishAssets", {
156
+ actionName: "PublishAssets",
157
+ runOrder: 2,
158
+ input: buildArtifact,
159
+ manifestPath: scope.buildDir,
160
+ }),
161
+ new CloudFormationCreateReplaceChangeSetAction({
162
+ actionName: "PrepareChanges",
163
+ runOrder: 3,
164
+ stackName: props.stackName ? props.stackName : props.stack.stackName,
165
+ account: props.account,
166
+ region: props.region,
167
+ changeSetName: `${props.stack.stackName}Changes`,
168
+ adminPermissions: true,
169
+ templatePath: buildArtifact.atPath(
170
+ path.join(scope.buildDir, props.stack.templateFile)
171
+ ),
172
+ }),
173
+ ...(props.manualApproval
174
+ ? [
175
+ new ManualApprovalAction({
176
+ actionName: "ApproveChanges",
177
+ runOrder: 4,
178
+ }),
179
+ ]
180
+ : []),
181
+ new CloudFormationExecuteChangeSetAction({
182
+ actionName: "ExecuteChanges",
183
+ runOrder: props.manualApproval ? 5 : 4,
184
+ stackName: props.stackName ? props.stackName : props.stack.stackName,
185
+ account: props.account,
186
+ region: props.region,
187
+ changeSetName: `${props.stack.stackName}Changes`,
188
+ output: props.output,
189
+ outputFileName: props.output
190
+ ? `${props.stack.stackName}Output.json`
191
+ : undefined,
192
+ }),
193
+ ];
194
+ }
195
+ }