-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline-stack.ts
174 lines (155 loc) · 7.26 KB
/
pipeline-stack.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { Stack, StackProps, Stage, StageProps } from 'aws-cdk-lib';
import * as codebuild from 'aws-cdk-lib/aws-codebuild';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as pipelines from 'aws-cdk-lib/pipelines';
import type { Construct } from 'constructs';
import type { Account, Instance, SubInstance } from './config';
import type { CdkManager } from './manager';
export const filterNils = <A>(array: Array<A | undefined>): Array<A> => {
const filtered: Array<A> = [];
for (const item of array) {
if (typeof item !== 'undefined') {
filtered.push(item);
}
}
return filtered;
};
export interface CodeBuildContext {
commitSha?: string;
buildNumber?: string;
}
export class PipelineStack<A> extends Stack {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getSourceConnection(accountConfig: Account, pipelineConfig: Instance<A>): pipelines.IFileSetProducer {
throw new Error('Not implemented');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getInstallCommands(accountConfig: Account, pipelineConfig: Instance<A>): Array<string> {
throw new Error('Not implemented');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getCommands(accountConfig: Account, pipelineConfig: Instance<A>): Array<string> {
throw new Error('Not implemented');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
addStacksToDeploymentStage(stage: Stage, accountConfig: Account, pipelineConfig: SubInstance<A>, codeBuildContext: CodeBuildContext): void {
throw new Error('Not implemented');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getStageProps(accountConfig: Account, pipelineConfig: SubInstance<A>): StageProps {
return {};
}
createStage(accountConfig: Account, pipelineConfig: SubInstance<A>): Stage {
const stage = new Stage(this, filterNils([accountConfig.name, pipelineConfig.suffix]).join('-'), this.getStageProps(accountConfig, pipelineConfig));
const codeBuildContext = {
commitSha: process.env['CODEBUILD_RESOLVED_SOURCE_VERSION'],
buildNumber: process.env['CODEBUILD_BUILD_NUMBER'],
};
this.addStacksToDeploymentStage(stage, accountConfig, pipelineConfig, codeBuildContext);
return stage;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getCodeBuildOptions(accountConfig: Account, pipelineConfig: Instance<A>): pipelines.CodeBuildOptions {
return {
buildEnvironment: {
buildImage: codebuild.LinuxBuildImage.STANDARD_6_0,
},
partialBuildSpec: codebuild.BuildSpec.fromObject({
phases: {
install: {
'runtime-versions': {
nodejs: '16.x',
},
},
},
}),
};
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getDockerCredentials(accountConfig: Account, pipelineConfig: SubInstance<A>): Array<pipelines.DockerCredential> {
return [];
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getPipelineSynthProjectAllowedRoleArns(accountConfig: Account, pipelineConfig: Instance<A>, stackProps: StackProps): string[] {
const cdkLookupRolePrefix = 'cdk-hnb659fds-lookup-role';
return [
`arn:aws:iam::${accountConfig.accountNumber}:role/${cdkLookupRolePrefix}-${accountConfig.accountNumber}-${this.region}`,
`arn:aws:iam::${this.account}:role/${cdkLookupRolePrefix}-${this.account}-${this.region}`,
];
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getOtherPipelineProps(accountConfig: Account, pipelineConfig: Instance<A>, stackProps: StackProps): Partial<pipelines.CodePipelineProps> {
return {};
}
public readonly manager: CdkManager<A>;
public readonly pipeline: pipelines.CodePipeline;
constructor(scope: Construct, id: string, manager: CdkManager<A>, accountConfig: Account, pipelineConfig: Instance<A>, stackProps: StackProps) {
super(scope, id, stackProps);
this.manager = manager;
this.pipeline = new pipelines.CodePipeline(this, 'pipeline', {
dockerEnabledForSynth: false,
synth: new pipelines.ShellStep('synth', {
input: this.getSourceConnection(accountConfig, pipelineConfig),
installCommands: this.getInstallCommands(accountConfig, pipelineConfig),
commands: this.getCommands(accountConfig, pipelineConfig),
env: {
TARGET_ACCOUNT: accountConfig.name,
...(pipelineConfig.suffix
? {
TARGET_ENVIRONMENT_SUFFIX: pipelineConfig.suffix,
}
: {}),
},
}),
crossAccountKeys: true,
codeBuildDefaults: this.getCodeBuildOptions(accountConfig, pipelineConfig),
dockerCredentials: this.getDockerCredentials(accountConfig, pipelineConfig),
assetPublishingCodeBuildDefaults: {
buildEnvironment: {
computeType: codebuild.ComputeType.MEDIUM,
},
},
...this.getOtherPipelineProps(accountConfig, pipelineConfig, stackProps),
});
if (pipelineConfig.sequencedInstances) {
for (const sequencedPipeline of pipelineConfig.sequencedInstances) {
const sequencedAccount = manager.getAccount(sequencedPipeline.accountName);
this.pipeline.addStage(
this.createStage(sequencedAccount, sequencedPipeline),
sequencedPipeline.requiresApproval
? {
pre: [
new pipelines.ManualApprovalStep(`DeployTo_${sequencedAccount.name}`, {
comment: `Deploy to ${sequencedAccount.name} (${sequencedPipeline.suffix}) (${sequencedAccount.accountNumber})`,
}),
],
}
: {},
);
}
}
this.pipeline.addStage(
this.createStage(accountConfig, pipelineConfig),
pipelineConfig.requiresApproval
? {
pre: [
new pipelines.ManualApprovalStep(`DeployTo_${accountConfig.name}`, {
comment: `Deploy to ${accountConfig.name} (${pipelineConfig.suffix}) (${accountConfig.accountNumber})`,
}),
],
}
: {},
);
this.pipeline.buildPipeline();
const allowedRoles = this.getPipelineSynthProjectAllowedRoleArns(accountConfig, pipelineConfig, stackProps);
if (allowedRoles.length > 0) {
this.pipeline.synthProject.addToRolePolicy(
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ['sts:AssumeRole'],
resources: allowedRoles,
}),
);
}
}
}