1+ import { Construct } from "constructs" ;
2+ import { BuildSpec , LinuxBuildImage , PipelineProject } from "aws-cdk-lib/aws-codebuild" ;
3+ import { IBucket } from "aws-cdk-lib/aws-s3" ;
4+ import { Artifact , Pipeline } from "aws-cdk-lib/aws-codepipeline" ;
5+ import {
6+ CodeBuildAction ,
7+ LambdaInvokeAction ,
8+ ManualApprovalAction ,
9+ S3DeployAction
10+ } from "aws-cdk-lib/aws-codepipeline-actions" ;
11+ import { IFunction } from "aws-cdk-lib/aws-lambda/lib/function-base" ;
12+
13+
14+ interface JavaBuildPipelineProps {
15+ repositoryName : string
16+ deployBucket : IBucket
17+ projectRoot ?: string
18+ deployBucketBasePath ?: string
19+ postActionLambda ?: IFunction
20+ }
21+
22+ export class JavaBuildPipeline extends Construct {
23+ constructor ( scope : Construct , id : string , props : JavaBuildPipelineProps ) {
24+ super ( scope , id ) ;
25+
26+ let directory = "." ;
27+ let s3BasePath = "jars" ;
28+ if ( props . projectRoot ) {
29+ directory = props . projectRoot ;
30+ }
31+ if ( props . deployBucketBasePath ) {
32+ s3BasePath = props . deployBucketBasePath
33+ }
34+
35+ const sourceAsset = new Artifact ( ) ;
36+ const defaultBuildSpec = BuildSpec . fromObject ( {
37+ version : '0.2' ,
38+ phases : {
39+ install : {
40+ "runtime-versions" : {
41+ "java" : "corretto11"
42+ }
43+ } ,
44+ build : {
45+ commands : [
46+ `curl ${ props . repositoryName } --output app.zip` , // Download zip directly from Github
47+ 'unzip app.zip' ,
48+ `cd ${ directory } ` ,
49+ 'mvn clean package -B' ,
50+ `mkdir -p ${ s3BasePath } ` ,
51+ `cp target/*.jar ${ s3BasePath } /`
52+ ]
53+ }
54+ } ,
55+ artifacts : {
56+ files : [
57+ `${ s3BasePath } /*.jar`
58+ ] ,
59+ 'discard-paths' : false ,
60+ 'base-directory' : directory
61+ }
62+ } ) ;
63+
64+ const project = new PipelineProject ( this , 'Pipeline' , {
65+ environment : {
66+ buildImage : LinuxBuildImage . STANDARD_5_0
67+ } ,
68+ buildSpec : defaultBuildSpec
69+ } ) ;
70+
71+ const buildOutput = new Artifact ( ) ;
72+
73+ const pipeline = new Pipeline ( this , 'CodePipeline' , {
74+ stages : [
75+ // In real world use code snippet like below to work with repository
76+ //
77+ // {
78+ // stageName: "source", actions: [new GitHubSourceAction({
79+ // actionName: "CodeCheckout",
80+ // oauthToken: SecretValue.secretsManager("github-secret"),
81+ // output: sourceAsset,
82+ // owner: "some-owner",
83+ // repo: props.repositoryName
84+ // })]
85+ // },
86+ {
87+ stageName : "build" , actions : [ new CodeBuildAction ( {
88+ actionName : "CodeBuild" , input : sourceAsset , project : project , outputs : [ buildOutput ]
89+ } ) ]
90+ } , {
91+ stageName : "saveArtefact" , actions : [ new S3DeployAction ( {
92+ bucket : props . deployBucket ,
93+ actionName : "SaveArtefact" ,
94+ input : buildOutput ,
95+ extract : true
96+ } ) ]
97+ } , {
98+ stageName : "approval" ,
99+ actions : [ new ManualApprovalAction ( {
100+ actionName : "Manual"
101+ } ) ]
102+ } ]
103+ } ) ;
104+
105+ if ( props . postActionLambda ) {
106+ pipeline . addStage ( {
107+ stageName : "deploy" , actions : [ new LambdaInvokeAction ( {
108+ actionName : "Deploy" ,
109+ lambda : props . postActionLambda
110+ } ) ]
111+ } ) ;
112+ }
113+ }
114+ }
0 commit comments