-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
.projenrc.ts
164 lines (152 loc) · 5.39 KB
/
.projenrc.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
152
153
154
155
156
157
158
159
160
161
162
163
164
import { awscdk } from 'projen';
import { DependabotScheduleInterval } from 'projen/lib/github';
import { NodePackageManager } from 'projen/lib/javascript';
import { YamlFile } from 'projen/lib/yaml';
import { createCdkDeploymentWorkflows } from './src/bin/cicd-helper';
import { addCdkActionTask } from './src/bin/env-helper';
// Set the minimum node version for AWS CDK and the GitHub actions workflow
const nodeVersion = '20.0.0';
/* Define the AWS region for the CDK app and github workflows
Default to us-east-1 if AWS_REGION is not set in your environment variables */
const awsRegion = process.env.AWS_REGION || 'us-east-1';
/**
* Define the name of the GitHub deploy role that will be created by the GitHubOIDCStack.
* Set this as an environment variable for the projen tasks, so other parts of the project
* can reference the role name.
* The default role name is 'GitHubActionsServiceRole'.
*/
const githubRole = 'GitHubActionsServiceRole';
/**
* Creates a new AWS CDK TypeScript application project.
*/
const project = new awscdk.AwsCdkTypeScriptApp({
authorName: 'Danny Steenman',
authorUrl: 'https://towardsthecloud.com',
authorEmail: '[email protected]',
authorOrganization: true,
name: 'aws-cdk-starterkit',
description: 'Create and deploy an AWS CDK app on your AWS account in less than 5 minutes using GitHub actions!',
cdkVersion: '2.163.1', // Find the latest CDK version here: https://www.npmjs.com/package/aws-cdk-lib
cdkVersionPinning: true,
defaultReleaseBranch: 'main',
packageManager: NodePackageManager.NPM,
minNodeVersion: nodeVersion,
projenVersion: '0.88.6', // Find the latest projen version here: https://www.npmjs.com/package/projen
projenrcTs: true,
buildWorkflow: false,
release: true,
deps: ['aws-cdk-github-oidc', 'cloudstructs'] /* Runtime dependencies of this module. */,
pullRequestTemplate: false,
autoApproveOptions: {
allowedUsernames: ['dependabot', 'dependabot[bot]', 'github-bot', 'github-actions[bot]'],
/**
* The name of the secret that has the GitHub PAT for auto-approving PRs.
* Generate a new PAT (https://github.com/settings/tokens/new) and add it to your repo's secrets
*/
secret: 'PROJEN_GITHUB_TOKEN',
},
dependabot: true,
dependabotOptions: {
scheduleInterval: DependabotScheduleInterval.WEEKLY,
labels: ['dependencies', 'auto-approve'],
groups: {
default: {
patterns: ['*'],
excludePatterns: ['aws-cdk*', 'projen'],
},
},
ignore: [{ dependencyName: 'aws-cdk-lib' }, { dependencyName: 'aws-cdk' }],
},
githubOptions: {
pullRequestLintOptions: {
semanticTitleOptions: {
types: ['feat', 'fix', 'build', 'chore', 'ci', 'docs', 'style', 'refactor'],
},
},
},
gitignore: [
'__pycache__',
'__pycache__/',
'!.eslintrc.js',
'.cache',
'.coverage.*',
'.coverage',
'.DS_Store',
'.env',
'.mypy_cache',
'.pytest_cache',
'.Python',
'.venv/',
'.vscode',
'*.js',
'*.log',
'*.manifest',
'*.pyc',
'*.spec',
'*.zip',
'**/cdk-test-report.xml',
'*node_modules*',
'build/',
'coverage/',
'dist/',
'downloads/',
'env/',
'ENV/',
'htmlcov/',
'sdist/',
'var/',
'venv/',
],
});
// Override auto-approve workflow to use the new version when autoApproveOptions is set
const autoApproveWorkflow = project.tryFindObjectFile('.github/workflows/auto-approve.yml');
if (autoApproveWorkflow && autoApproveWorkflow instanceof YamlFile) {
autoApproveWorkflow.addOverride('jobs.approve.steps.0.uses', 'hmarr/auto-approve-action@v4');
}
// Define the target AWS accounts for the different environments
type Environment = 'test' | 'production';
interface EnvironmentConfig {
accountId: string;
enableBranchDeploy: boolean;
}
const environmentConfigs: Record<Environment, EnvironmentConfig> = {
test: { accountId: '987654321012', enableBranchDeploy: true },
production: { accountId: '123456789012', enableBranchDeploy: false },
};
/* Add npm run commands that you can use to deploy to each environment
The environment variables are passed to the CDK CLI to deploy to the correct account and region
The `cdkDeploymentTask` function is defined in the `src/bin/helper.ts` file
You can now run a command like: `npm run dev:synth` to synthesize your aws cdk dev stacks */
const github = project.github;
if (github) {
for (const [env, config] of Object.entries(environmentConfigs) as [Environment, EnvironmentConfig][]) {
// Adds customized 'npm run' commands for executing cdk synth, test, deploy and diff for each environment
addCdkActionTask(project, {
CDK_DEFAULT_ACCOUNT: config.accountId,
CDK_DEFAULT_REGION: awsRegion,
ENVIRONMENT: env,
GITHUB_DEPLOY_ROLE: githubRole,
});
// If branch deployment is enabled for this environment, add the GIT_BRANCH_REF task
if (config.enableBranchDeploy) {
addCdkActionTask(project, {
CDK_DEFAULT_ACCOUNT: config.accountId,
CDK_DEFAULT_REGION: awsRegion,
ENVIRONMENT: env,
GITHUB_DEPLOY_ROLE: githubRole,
GIT_BRANCH_REF: '$(git rev-parse --abbrev-ref HEAD)',
});
}
// Adds GitHub action workflows for deploying the CDK stacks to the target AWS account
createCdkDeploymentWorkflows(
github,
config.accountId,
awsRegion,
env,
githubRole,
nodeVersion,
config.enableBranchDeploy,
);
}
}
project.synth();