forked from cdklabs/cdk-pipelines-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-app.ts
150 lines (133 loc) · 4.22 KB
/
example-app.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
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { App, CfnOutput, RemovalPolicy, Stack, Stage, StageProps } from 'aws-cdk-lib';
import * as codebuild from 'aws-cdk-lib/aws-codebuild';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as s3 from 'aws-cdk-lib/aws-s3';
import { EnvironmentUtils } from 'aws-cdk-lib/cx-api';
import { ShellStep } from 'aws-cdk-lib/pipelines';
import { GitHubWorkflow, DockerCredential, YamlFile } from '../src';
export interface GitHubExampleAppProps {
/**
* The root directory of the repository.
*
* A `cdk.out` directory and `.github/workflows/deploy.yml` file will be
* synthesied into this directory.
*/
readonly repoDir: string;
/**
* AWS Environment for stage A.
*
* Environment must be bootstrapped with `CDK_NEW_BOOTSTRAP=1`.
*
* @example aws://111111111111/us-east-1
*/
readonly envA: string;
/**
* AWS environment for stage B.
*
* Environment must be bootstrapped with `CDK_NEW_BOOTSTRAP=1`.
*
* @example aws://111111111111/us-east-2
*/
readonly envB: string;
}
/**
* A CDK app that uses GitHub engine backend for CDK Pipelines.
*
* Specify the account
*
* You will need to bootstrap (with `CDK_NEW_BOOTSTRAP=1`) two environments
*/
export class GitHubExampleApp extends App {
public workflowFile: YamlFile;
constructor(props: GitHubExampleAppProps) {
const repoDir = props.repoDir ?? fs.mkdtempSync(path.join(os.tmpdir(), 'github-engine.'));
super({
outdir: path.join(repoDir, 'cdk.out'),
context: {
'@aws-cdk/core:newStyleStackSynthesis': '1',
},
stackTraces: false,
autoSynth: false,
treeMetadata: false,
});
const workflowsDir = path.join(repoDir, '.github/workflows');
fs.mkdirSync(workflowsDir, { recursive: true });
const pipeline = new GitHubWorkflow(this, 'Pipeline', {
synth: new ShellStep('Build', {
commands: ['echo "nothing to do (cdk.out is committed)"'],
}),
workflowPath: path.join(workflowsDir, 'deploy.yml'),
preSynthed: true,
buildContainer: { image: 'alpine' },
preBuildSteps: [
{
uses: 'actions/setup-node@v3',
with: { nodeVersion: '16' },
},
],
postBuildSteps: [
{ run: 'echo post-build' },
],
dockerCredentials: [
DockerCredential.ecr('000000000000.dkr.ecr.us-east-1.amazonaws.com'),
],
});
this.workflowFile = pipeline.workflowFile;
const myStage = new MyStage(this, 'StageA', { env: EnvironmentUtils.parse(props.envA) });
pipeline.addStage(myStage, {
pre: [new ShellStep('Pre', {
commands: ['echo hello'],
})],
post: [new ShellStep('Post', {
envFromCfnOutputs: {
FN_NAME: myStage.fnName,
},
commands: ['echo FN_NAME equals: $FN_NAME'],
})],
});
pipeline.addStage(new MyStage(this, 'StageB', { env: EnvironmentUtils.parse(props.envB) }));
}
}
class MyStage extends Stage {
public readonly fnName: CfnOutput;
constructor(scope: App, id: string, props: StageProps) {
super(scope, id, props);
const fnStack = new Stack(this, 'FunctionStack');
const bucketStack = new Stack(this, 'BucketStack');
const bucket = new s3.Bucket(bucketStack, 'Bucket', {
autoDeleteObjects: true,
removalPolicy: RemovalPolicy.DESTROY,
});
new codebuild.Project(fnStack, 'MyProject', {
buildSpec: codebuild.BuildSpec.fromObject({
version: '0.2',
phases: {
build: {
commands: ['ls'],
},
},
}),
grantReportGroupPermissions: false,
environment: {
buildImage: codebuild.LinuxBuildImage.fromAsset(fnStack, 'MyImage', {
directory: path.join(__dirname, 'demo-image'),
}),
},
});
const fn = new lambda.Function(fnStack, 'Function', {
code: lambda.Code.fromAsset(path.join(__dirname, 'fixtures')),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_14_X,
environment: {
BUCKET_NAME: bucket.bucketName, // <-- cross stack reference
},
});
this.fnName = new CfnOutput(fnStack, 'myout', {
value: fn.functionName,
});
bucket.grantRead(fn);
}
}