/
main.ts
86 lines (72 loc) · 2.5 KB
/
main.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
import * as path from 'path';
import * as lambda from '@aws-cdk/aws-lambda';
import * as s3 from '@aws-cdk/aws-s3';
import { App, Construct, Stack, StackProps, CfnOutput } from '@aws-cdk/core';
export class MyStack extends Stack {
constructor(scope: Construct, id: string, props: StackProps = {}) {
super(scope, id, props);
this.createCodePipelineEventLambdaFunction(
'main',
'',
'pipeline.pipelineName',
'',
);
}
private createCodePipelineEventLambdaFunction(
stage: string,
slackWebhookURL: string,
codePipelineName: string,
githubPersonalToken: string,
): lambda.Function {
const badgeBucket = new s3.Bucket(this, 'BadgeBucket', {
publicReadAccess: true,
});
const badgeBucketImageKeyName = `${stage}-latest-build.svg`;
const lambdaFunc = new lambda.Function(this, 'CodepipelineEventLambda', {
code: lambda.Code.fromAsset(
path.join(__dirname, '../lambda'),
{
bundling: {
user: 'root',
image: lambda.Runtime.NODEJS_14_X.bundlingImage,
command: [
'bash',
'-c',
[
'npm install',
'npm run build',
'cp -r /asset-input/dist /asset-output/',
'npm install --only=production',
'cp -a /asset-input/node_modules /asset-output/',
].join(' && '),
],
},
},
),
runtime: lambda.Runtime.NODEJS_14_X,
handler: 'dist/codepipelineEventLambda.handler',
environment: {
STAGE: stage,
SLACK_WEBHOOK_URL: slackWebhookURL,
BADGE_BUCKET_NAME: badgeBucket.bucketName,
BADGE_BUCKET_IMAGE_KEY_NAME: badgeBucketImageKeyName,
CODE_PIPELINE_NAME: codePipelineName,
GITHUB_PERSONAL_TOKEN: githubPersonalToken,
},
});
badgeBucket.grantReadWrite(lambdaFunc);
new CfnOutput(this, 'badgeMarkdownLink', {
value: `[![Build Status](https://${badgeBucket.bucketName}.s3-ap-northeast-1.amazonaws.com/${badgeBucketImageKeyName}#1)](https://ap-northeast-1.console.aws.amazon.com/codesuite/codepipeline/pipelines/${codePipelineName}/view)`,
});
return lambdaFunc;
}
}
// for development, use account/region from cdk cli
const devEnv = {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION,
};
const app = new App();
new MyStack(app, 'my-stack-dev', { env: devEnv });
// new MyStack(app, 'my-stack-prod', { env: prodEnv });
app.synth();