@cdklabs/deploy-time-build 0.0.1 → 0.0.2
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.
- package/.jsii +9563 -0
- package/.jsii.tabl.json +1 -0
- package/API.md +1673 -0
- package/AmazonQ.md +65 -0
- package/LICENSE +202 -0
- package/README.md +212 -45
- package/example/README.md +18 -0
- package/example/cdk.json +3 -0
- package/example/example-app/index.html +12 -0
- package/example/example-app/package-lock.json +1804 -0
- package/example/example-app/package.json +21 -0
- package/example/example-app/src/App.css +42 -0
- package/example/example-app/src/App.tsx +43 -0
- package/example/example-app/src/index.css +13 -0
- package/example/example-app/src/main.tsx +10 -0
- package/example/example-app/src/vite.env.d.ts +1 -0
- package/example/example-app/tsconfig.json +21 -0
- package/example/example-app/tsconfig.node.json +8 -0
- package/example/example-app/vite.config.ts +7 -0
- package/example/example-image/Dockerfile +6 -0
- package/example/index.ts +156 -0
- package/imgs/architecture.png +0 -0
- package/imgs/demo.png +0 -0
- package/imgs/soci-architecture.png +0 -0
- package/lambda/trigger-codebuild/dist/index.js +279 -0
- package/lambda/trigger-codebuild/index.ts +269 -0
- package/lambda/trigger-codebuild/package-lock.json +2904 -0
- package/lambda/trigger-codebuild/package.json +17 -0
- package/lib/container-image-build.d.ts +84 -0
- package/lib/container-image-build.js +221 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +21 -0
- package/lib/nodejs-build.d.ts +84 -0
- package/lib/nodejs-build.js +221 -0
- package/lib/singleton-project.d.ts +28 -0
- package/lib/singleton-project.js +43 -0
- package/lib/soci-index-build.d.ts +30 -0
- package/lib/soci-index-build.js +118 -0
- package/lib/soci-index-v2-build.d.ts +41 -0
- package/lib/soci-index-v2-build.js +131 -0
- package/lib/types.d.ts +44 -0
- package/lib/types.js +3 -0
- package/package.json +156 -6
- package/rosetta/default.ts-fixture +24 -0
- package/rosetta/imported.ts-fixture +25 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { CodeBuildClient, StartBuildCommand } from '@aws-sdk/client-codebuild';
|
|
2
|
+
import { CloudFormationCustomResourceEvent, CloudFormationCustomResourceResponse, Context } from 'aws-lambda';
|
|
3
|
+
import Crypto from 'crypto';
|
|
4
|
+
import { setTimeout } from 'timers/promises';
|
|
5
|
+
import type { ResourceProperties } from '../../src/types';
|
|
6
|
+
|
|
7
|
+
const cb = new CodeBuildClient({});
|
|
8
|
+
|
|
9
|
+
type Event = CloudFormationCustomResourceEvent & {
|
|
10
|
+
ResourceProperties: ResourceProperties;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const handler = async (event: Event, context: Context) => {
|
|
14
|
+
try {
|
|
15
|
+
if (event.RequestType == 'Create' || event.RequestType == 'Update') {
|
|
16
|
+
const props = event.ResourceProperties;
|
|
17
|
+
const commonEnvironments = [
|
|
18
|
+
{
|
|
19
|
+
name: 'responseURL',
|
|
20
|
+
value: event.ResponseURL,
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
name: 'stackId',
|
|
24
|
+
value: event.StackId,
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
name: 'requestId',
|
|
28
|
+
value: event.RequestId,
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: 'logicalResourceId',
|
|
32
|
+
value: event.LogicalResourceId,
|
|
33
|
+
},
|
|
34
|
+
];
|
|
35
|
+
const newPhysicalId = Crypto.randomBytes(16).toString('hex');
|
|
36
|
+
|
|
37
|
+
let command: StartBuildCommand;
|
|
38
|
+
switch (props.type) {
|
|
39
|
+
case 'NodejsBuild':
|
|
40
|
+
command = new StartBuildCommand({
|
|
41
|
+
projectName: props.codeBuildProjectName,
|
|
42
|
+
environmentVariablesOverride: [
|
|
43
|
+
...commonEnvironments,
|
|
44
|
+
{
|
|
45
|
+
name: 'input',
|
|
46
|
+
value: JSON.stringify(
|
|
47
|
+
props.sources.map((source) => ({
|
|
48
|
+
assetUrl: `s3://${source.sourceBucketName}/${source.sourceObjectKey}`,
|
|
49
|
+
extractPath: source.extractPath,
|
|
50
|
+
commands: (source.commands ?? []).join(' && '),
|
|
51
|
+
}))
|
|
52
|
+
),
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: 'buildCommands',
|
|
56
|
+
value: props.buildCommands.join(' && '),
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: 'destinationBucketName',
|
|
60
|
+
value: props.destinationBucketName,
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
name: 'destinationKeyPrefix',
|
|
64
|
+
// remove the beginning / if any
|
|
65
|
+
value: props.destinationKeyPrefix.startsWith('/') ? props.destinationKeyPrefix.slice(1) : props.destinationKeyPrefix,
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
name: 'distributionId',
|
|
69
|
+
value: props.distributionId ?? '',
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
name: 'distributionPath',
|
|
73
|
+
value: (() => {
|
|
74
|
+
let path = props.destinationKeyPrefix;
|
|
75
|
+
if (!path.startsWith('/')) {
|
|
76
|
+
path = '/' + path;
|
|
77
|
+
}
|
|
78
|
+
if (!path.endsWith('/')) {
|
|
79
|
+
path += '/';
|
|
80
|
+
}
|
|
81
|
+
return path + '*';
|
|
82
|
+
})(),
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
name: 'assetBucketName',
|
|
86
|
+
value: props.assetBucketName,
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: 'workingDirectory',
|
|
90
|
+
value: props.workingDirectory,
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
name: 'outputSourceDirectory',
|
|
94
|
+
value: props.outputSourceDirectory,
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
name: 'projectName',
|
|
98
|
+
value: props.codeBuildProjectName,
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
name: 'outputEnvFile',
|
|
102
|
+
value: props.outputEnvFile.toString(),
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
name: 'envFileKey',
|
|
106
|
+
value: `deploy-time-build/${event.StackId.split('/')[1]}/${event.LogicalResourceId}/${newPhysicalId}.env`,
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
name: 'envNames',
|
|
110
|
+
value: Object.keys(props.environment ?? {}).join(','),
|
|
111
|
+
},
|
|
112
|
+
...Object.entries(props.environment ?? {}).map(([name, value]) => ({
|
|
113
|
+
name,
|
|
114
|
+
value,
|
|
115
|
+
})),
|
|
116
|
+
],
|
|
117
|
+
});
|
|
118
|
+
break;
|
|
119
|
+
case 'SociIndexBuild':
|
|
120
|
+
command = new StartBuildCommand({
|
|
121
|
+
projectName: props.codeBuildProjectName,
|
|
122
|
+
environmentVariablesOverride: [
|
|
123
|
+
...commonEnvironments,
|
|
124
|
+
{
|
|
125
|
+
name: 'repositoryName',
|
|
126
|
+
value: props.repositoryName,
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
name: 'imageTag',
|
|
130
|
+
value: props.imageTag,
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
name: 'projectName',
|
|
134
|
+
value: props.codeBuildProjectName,
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
});
|
|
138
|
+
break;
|
|
139
|
+
case 'SociIndexV2Build':
|
|
140
|
+
command = new StartBuildCommand({
|
|
141
|
+
projectName: props.codeBuildProjectName,
|
|
142
|
+
environmentVariablesOverride: [
|
|
143
|
+
...commonEnvironments,
|
|
144
|
+
{
|
|
145
|
+
name: 'repositoryName',
|
|
146
|
+
value: props.repositoryName,
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
name: 'inputImageTag',
|
|
150
|
+
value: props.inputImageTag,
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
name: 'outputImageTag',
|
|
154
|
+
value: props.outputImageTag,
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
name: 'projectName',
|
|
158
|
+
value: props.codeBuildProjectName,
|
|
159
|
+
},
|
|
160
|
+
],
|
|
161
|
+
});
|
|
162
|
+
break;
|
|
163
|
+
case 'ContainerImageBuild': {
|
|
164
|
+
const imageTag = props.imageTag ?? `${props.tagPrefix ?? ''}${newPhysicalId}`;
|
|
165
|
+
command = new StartBuildCommand({
|
|
166
|
+
projectName: props.codeBuildProjectName,
|
|
167
|
+
environmentVariablesOverride: [
|
|
168
|
+
...commonEnvironments,
|
|
169
|
+
{
|
|
170
|
+
name: 'repositoryUri',
|
|
171
|
+
value: props.repositoryUri,
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
name: 'repositoryAuthUri',
|
|
175
|
+
value: props.repositoryUri.split('/')[0],
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
name: 'repositoryRegion',
|
|
179
|
+
value: props.repositoryUri.split('.')[3],
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
name: 'buildCommand',
|
|
183
|
+
value: props.buildCommand,
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
name: 'imageTag',
|
|
187
|
+
value: imageTag,
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
name: 'projectName',
|
|
191
|
+
value: props.codeBuildProjectName,
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
name: 'sourceS3Url',
|
|
195
|
+
value: props.sourceS3Url,
|
|
196
|
+
},
|
|
197
|
+
],
|
|
198
|
+
});
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
default:
|
|
202
|
+
throw new Error(`invalid event type ${props}}`);
|
|
203
|
+
}
|
|
204
|
+
// start code build project
|
|
205
|
+
let retries = 0;
|
|
206
|
+
while (retries < 10) {
|
|
207
|
+
try {
|
|
208
|
+
await cb.send(command);
|
|
209
|
+
break;
|
|
210
|
+
} catch (error: any) {
|
|
211
|
+
// Sometimes AccessDeniedException is thrown due to IAM propagation delay. It should be resolved with retry.
|
|
212
|
+
if (error.name === 'AccessDeniedException') {
|
|
213
|
+
retries++;
|
|
214
|
+
console.log(`AccessDeniedException encountered, retrying (${retries})...`);
|
|
215
|
+
await setTimeout(5000);
|
|
216
|
+
} else {
|
|
217
|
+
throw error;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Sometimes CodeBuild build fails before running buildspec, without calling the CFn callback.
|
|
223
|
+
// We can poll the status of a build for a few minutes and sendStatus if such errors are detected.
|
|
224
|
+
// if (build.build?.id == null) {
|
|
225
|
+
// throw new Error('build id is null');
|
|
226
|
+
// }
|
|
227
|
+
|
|
228
|
+
// for (let i=0; i< 20; i++) {
|
|
229
|
+
// const res = await cb.send(new BatchGetBuildsCommand({ ids: [build.build.id] }));
|
|
230
|
+
// const status = res.builds?.[0].buildStatus;
|
|
231
|
+
// if (status == null) {
|
|
232
|
+
// throw new Error('build status is null');
|
|
233
|
+
// }
|
|
234
|
+
|
|
235
|
+
// await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
236
|
+
// }
|
|
237
|
+
} else {
|
|
238
|
+
// Do nothing on a DELETE event.
|
|
239
|
+
await sendStatus('SUCCESS', event, context);
|
|
240
|
+
}
|
|
241
|
+
} catch (e) {
|
|
242
|
+
console.log(e);
|
|
243
|
+
const err = e as Error;
|
|
244
|
+
await sendStatus('FAILED', event, context, err.message);
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
const sendStatus = async (status: 'SUCCESS' | 'FAILED', event: Event, context: Context, reason?: string) => {
|
|
249
|
+
const responseBody: CloudFormationCustomResourceResponse = {
|
|
250
|
+
Status: status,
|
|
251
|
+
Reason: reason ?? 'See the details in CloudWatch Log Stream: ' + context.logStreamName,
|
|
252
|
+
PhysicalResourceId: context.logStreamName,
|
|
253
|
+
StackId: event.StackId,
|
|
254
|
+
RequestId: event.RequestId,
|
|
255
|
+
LogicalResourceId: event.LogicalResourceId,
|
|
256
|
+
NoEcho: false,
|
|
257
|
+
Data: {}, //responseData
|
|
258
|
+
};
|
|
259
|
+
const responseBodyString = JSON.stringify(responseBody);
|
|
260
|
+
|
|
261
|
+
await fetch(event.ResponseURL, {
|
|
262
|
+
method: 'PUT',
|
|
263
|
+
body: responseBodyString,
|
|
264
|
+
headers: {
|
|
265
|
+
'Content-Type': '',
|
|
266
|
+
'Content-Length': responseBodyString.length.toString(),
|
|
267
|
+
},
|
|
268
|
+
});
|
|
269
|
+
};
|