@@ -6,17 +6,22 @@ import {
66 CodeBuildAction ,
77 LambdaInvokeAction ,
88 ManualApprovalAction ,
9- S3DeployAction
9+ S3DeployAction ,
10+ S3SourceAction
1011} from "aws-cdk-lib/aws-codepipeline-actions" ;
11- import { IFunction } from "aws-cdk-lib/aws-lambda/lib/function-base" ;
12+ import * as path from "path" ;
13+ import { Code , Function , Runtime } from "aws-cdk-lib/aws-lambda" ;
14+ import { Aws , Duration } from "aws-cdk-lib" ;
15+ import { PolicyStatement } from "aws-cdk-lib/aws-iam" ;
16+ import { SOURCE_CODE_ZIP } from "../shared-vars" ;
1217
1318
1419interface JavaBuildPipelineProps {
20+ appName : string
1521 repositoryName : string
1622 deployBucket : IBucket
1723 projectRoot ?: string
1824 deployBucketBasePath ?: string
19- postActionLambda ?: IFunction
2025}
2126
2227export class JavaBuildPipeline extends Construct {
@@ -43,8 +48,6 @@ export class JavaBuildPipeline extends Construct {
4348 } ,
4449 build : {
4550 commands : [
46- `curl ${ props . repositoryName } --output app.zip` , // Download zip directly from Github
47- 'unzip app.zip' ,
4851 `cd ${ directory } ` ,
4952 'mvn clean package -B' ,
5053 `mkdir -p ${ s3BasePath } ` ,
@@ -83,9 +86,17 @@ export class JavaBuildPipeline extends Construct {
8386 // repo: props.repositoryName
8487 // })]
8588 // },
89+ {
90+ stageName : "source" , actions : [ new S3SourceAction ( {
91+ output : sourceAsset ,
92+ actionName : "Checkout" ,
93+ bucket : props . deployBucket ,
94+ bucketKey : SOURCE_CODE_ZIP
95+ } ) ]
96+ } ,
8697 {
8798 stageName : "build" , actions : [ new CodeBuildAction ( {
88- actionName : "CodeBuild" , input : sourceAsset , project : project , outputs : [ buildOutput ]
99+ input : sourceAsset , actionName : "CodeBuild" , project : project , outputs : [ buildOutput ]
89100 } ) ]
90101 } , {
91102 stageName : "saveArtefact" , actions : [ new S3DeployAction ( {
@@ -102,13 +113,33 @@ export class JavaBuildPipeline extends Construct {
102113 } ]
103114 } ) ;
104115
105- if ( props . postActionLambda ) {
106- pipeline . addStage ( {
107- stageName : "deploy" , actions : [ new LambdaInvokeAction ( {
108- actionName : "Deploy" ,
109- lambda : props . postActionLambda
110- } ) ]
111- } ) ;
112- }
116+ const versionUpdateFn = new Function ( this , 'version-update-fn' , {
117+ code : Code . fromAsset ( path . join ( __dirname , '../../flink-app-redeploy-hook' ) ) ,
118+ handler : "app.lambda_handler" ,
119+ runtime : Runtime . PYTHON_3_9 ,
120+ environment : {
121+ ASSET_BUCKET_ARN : props . deployBucket . bucketArn ,
122+ FILE_KEY : s3BasePath + "/" + props . appName + "-latest.jar" ,
123+ APP_NAME : props . appName
124+ } ,
125+ timeout : Duration . minutes ( 1 )
126+ } ) ;
127+
128+ versionUpdateFn . addToRolePolicy ( new PolicyStatement ( {
129+ actions : [ "kinesisanalytics:DescribeApplication" , "kinesisanalytics:UpdateApplication" ] ,
130+ resources : [ "arn:aws:kinesisanalytics:" + Aws . REGION + ":" + Aws . ACCOUNT_ID + ":application/*" ]
131+ } ) ) ;
132+ versionUpdateFn . addToRolePolicy ( new PolicyStatement ( {
133+ resources : [ "*" ] ,
134+ actions : [ "codepipeline:PutJobSuccessResult" , "codepipeline:PutJobFailureResult" ]
135+ } ) ) ;
136+
137+ pipeline . addStage ( {
138+ stageName : "deploy" , actions : [ new LambdaInvokeAction ( {
139+ actionName : "Deploy" ,
140+ lambda : versionUpdateFn
141+ } ) ]
142+ } ) ;
143+
113144 }
114145}
0 commit comments