-
Notifications
You must be signed in to change notification settings - Fork 2
/
python-build-pipeline.ts
151 lines (137 loc) · 5.33 KB
/
python-build-pipeline.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
import {Construct} from "constructs";
import {BuildSpec, LinuxBuildImage, PipelineProject} from "aws-cdk-lib/aws-codebuild";
import {IBucket} from "aws-cdk-lib/aws-s3";
import {Artifact, Pipeline} from "aws-cdk-lib/aws-codepipeline";
import {
CodeBuildAction,
LambdaInvokeAction,
ManualApprovalAction,
S3DeployAction,
S3SourceAction
} from "aws-cdk-lib/aws-codepipeline-actions";
import {Code, Function, Runtime} from "aws-cdk-lib/aws-lambda";
import {Aws, Duration} from "aws-cdk-lib";
import {PolicyStatement} from "aws-cdk-lib/aws-iam";
import {MANUAL_APPROVAL_REQUIRED, SOURCE_CODE_ZIP} from "../shared-vars";
interface PythonBuildPipelineProps {
appName: string
repositoryName: string
deployBucket: IBucket
projectRoot?: string
deployBucketBasePath?: string
}
export class PythonBuildPipeline extends Construct {
readonly pipeline: Pipeline
constructor(scope: Construct, id: string, props: PythonBuildPipelineProps) {
super(scope, id);
let directory = ".";
let s3BasePath = "python-binaries";
if (props.projectRoot) {
directory = props.projectRoot;
}
if (props.deployBucketBasePath) {
s3BasePath = props.deployBucketBasePath
}
const sourceAsset = new Artifact();
const defaultBuildSpec = BuildSpec.fromObject({
version: '0.2',
phases: {
install: {
"runtime-versions": {
"python": "3.x"
}
},
build: {
commands: [
`cd ${directory}`,
'mkdir dependencies',
'pip3 install -r requirements.txt -t dependencies',
`mkdir -p ${s3BasePath}`,
`zip -r ${s3BasePath}/${props.appName}-latest.zip *.py lib/* dependencies`
]
}
},
artifacts: {
files: [
`${s3BasePath}/*.zip`
],
'discard-paths': false,
'base-directory': directory
}
});
const project = new PipelineProject(this, 'Pipeline', {
environment: {
buildImage: LinuxBuildImage.STANDARD_6_0
},
buildSpec: defaultBuildSpec
});
const buildOutput = new Artifact();
this.pipeline = new Pipeline(this, 'CodePipeline', {
stages: [
// In real world use code snippet like below to work with repository
//
// {
// stageName: "source", actions: [new GitHubSourceAction({
// actionName: "CodeCheckout",
// oauthToken: SecretValue.secretsManager("github-secret"),
// output: sourceAsset,
// owner: "some-owner",
// repo: props.repositoryName
// })]
// },
{
stageName: "source", actions: [new S3SourceAction({
output: sourceAsset,
actionName: "Checkout",
bucket: props.deployBucket,
bucketKey: SOURCE_CODE_ZIP
})]
},
{
stageName: "build", actions: [new CodeBuildAction({
input: sourceAsset, actionName: "CodeBuild", project: project, outputs: [buildOutput]
})]
}, {
stageName: "saveArtifact", actions: [new S3DeployAction({
bucket: props.deployBucket,
actionName: "SaveArtifact",
input: buildOutput,
extract: true
})]
}]
});
if (MANUAL_APPROVAL_REQUIRED) {
this.pipeline.addStage({
stageName: "approval",
actions: [new ManualApprovalAction({
actionName: "Manual"
})]
})
}
const versionUpdateFn = new Function(this, 'version-update-fn', {
code: Code.fromAsset('flink-app-redeploy-hook'),
handler: "app.lambda_handler",
runtime: Runtime.PYTHON_3_9,
environment: {
ASSET_BUCKET_ARN: props.deployBucket.bucketArn,
FILE_KEY: s3BasePath + "/" + props.appName + "-latest.zip",
APP_NAME: props.appName
},
timeout: Duration.minutes(1)
});
versionUpdateFn.addToRolePolicy(new PolicyStatement({
actions: ["kinesisanalytics:DescribeApplication", "kinesisanalytics:UpdateApplication"],
resources: ["arn:aws:kinesisanalytics:" + Aws.REGION + ":" + Aws.ACCOUNT_ID + ":application/" + props.appName]
}));
versionUpdateFn.addToRolePolicy(new PolicyStatement({
resources: ["*"],
actions: ["codepipeline:PutJobSuccessResult", "codepipeline:PutJobFailureResult"]
}));
this.pipeline.addStage({
stageName: "deploy", actions: [new LambdaInvokeAction({
actionName: "Deploy",
lambda: versionUpdateFn
})]
});
}
}