-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdk-pipelines-deployment-stack.ts
63 lines (55 loc) · 2.24 KB
/
cdk-pipelines-deployment-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
import * as cdk from 'aws-cdk-lib';
import * as codebuild from 'aws-cdk-lib/aws-codebuild';
import * as codepipeline from 'aws-cdk-lib/aws-codepipeline';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as pipelines from 'aws-cdk-lib/pipelines';
import { aws_codestarconnections as codestar_connections } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { FrontendResources } from '../resources/frontend-resources';
// CDK pipelines module を使って CI/CD Pipeline を構成する例
export class CdkPipelinesDeploymentStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const githubConnection = new codestar_connections.CfnConnection(this, 'GithubConnection', {
connectionName: 'CdkPipelinesConnection',
providerType: 'GitHub',
});
const githubOwnerName = this.node.tryGetContext('githubOwnerName');
const githubRepoName = this.node.tryGetContext('githubRepoName');
const artifactBucket = new s3.Bucket(this, 'ArtifactsBucket', {
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
const codePipeline = new codepipeline.Pipeline(this, 'CodePipeline', {
pipelineName: 'CdkDeploymentPipeline',
artifactBucket,
});
const pipeline = new pipelines.CodePipeline(this, 'CdkPipeline', {
selfMutation: true,
synthCodeBuildDefaults: {
partialBuildSpec: codebuild.BuildSpec.fromObject({
phases: {
pre_build: {
commands: `echo '{"githubOwnerName":"${githubOwnerName}","githubRepoName":"${githubRepoName}"}' >> cdk.context.json`,
},
},
}),
},
synth: new pipelines.ShellStep('Synth', {
input: pipelines.CodePipelineSource.connection(`${githubOwnerName}/${githubRepoName}`, 'main', {
connectionArn: githubConnection.ref,
}),
commands: ['npm i', 'npm run cdk synth'],
}),
codePipeline,
});
const frontendResources = new FrontendResources(this, 'FrontendResources');
pipeline.addStage(frontendResources, {
stackSteps: [
{
stack: frontendResources.dbStack,
changeSet: [new pipelines.ManualApprovalStep('ChangeSetApproval')],
},
],
});
}
}