-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgha-runners-stack.ts
92 lines (86 loc) · 3.03 KB
/
gha-runners-stack.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
import * as cdk from 'aws-cdk-lib'
import * as codebuild from 'aws-cdk-lib/aws-codebuild'
import * as iam from 'aws-cdk-lib/aws-iam'
import * as ssm from 'aws-cdk-lib/aws-ssm'
import type { Construct } from 'constructs'
import { CODEBUILD_POLICY_ARN_PARAM_NAME, GITHUB_CODECONNECTION_ARN_PARAM_NAME } from './constants'
/**
* This stack sets up CodeBuild projects to run GitHub Actions runners.
*/
export class GhaRunnersStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props)
const githubOrganisation = scope.node.tryGetContext('githuborg')
if (!githubOrganisation) {
throw new Error("Context variable 'githuborg' is required. This should be the name of your GitHub organisation")
}
const connectionArn = ssm.StringParameter.fromStringParameterName(
this,
'ConnectionArnParam',
GITHUB_CODECONNECTION_ARN_PARAM_NAME
).stringValue
const codeBuildPolicyArnParam = ssm.StringParameter.fromStringParameterName(
this,
'CodeBuildPolicyArnParam',
CODEBUILD_POLICY_ARN_PARAM_NAME
).stringValue
const projectServiceRole = new iam.Role(this, 'CodeBuildServiceRole', {
assumedBy: new iam.ServicePrincipal('codebuild.amazonaws.com'),
path: '/service-role/'
})
projectServiceRole.addManagedPolicy(
iam.ManagedPolicy.fromManagedPolicyArn(this, 'CodeBuildManagedPolicy', codeBuildPolicyArnParam)
)
const environmentsBySuffix: Record<string, codebuild.CfnProject.EnvironmentProperty> = {
'': {
computeType: 'BUILD_GENERAL1_SMALL',
image: 'aws/codebuild/standard:5.0',
type: 'LINUX_CONTAINER'
},
'-lambda': {
computeType: 'BUILD_LAMBDA_10GB',
image: 'aws/codebuild/amazonlinux-x86_64-lambda-standard:nodejs20',
type: 'LINUX_LAMBDA_CONTAINER'
}
}
const commonCodeBuildProjectProps: Omit<codebuild.CfnProjectProps, 'name' | 'environment'> = {
source: {
gitCloneDepth: 1,
type: 'GITHUB',
location: 'CODEBUILD_DEFAULT_WEBHOOK_SOURCE_LOCATION',
auth: {
// Change type to 'OAUTH' if you want to use a GitHub PAT that has already been
// loaded into your AWS account+region with `import-source-credentials`.
type: 'CODECONNECTIONS',
resource: connectionArn
}
},
triggers: {
webhook: true,
scopeConfiguration: {
name: githubOrganisation
},
filterGroups: [
[
{
type: 'EVENT',
pattern: 'WORKFLOW_JOB_QUEUED'
}
]
]
},
artifacts: {
type: 'NO_ARTIFACTS'
},
concurrentBuildLimit: 60,
serviceRole: projectServiceRole.roleArn
}
for (const [suffix, environment] of Object.entries(environmentsBySuffix)) {
new codebuild.CfnProject(this, `RunnerProject${suffix}`, {
...commonCodeBuildProjectProps,
environment: environment,
name: `gha-runners${suffix}`
})
}
}
}