-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcodebuild.ts
105 lines (93 loc) · 2.44 KB
/
codebuild.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
import * as aws from '@pulumi/aws';
import {
dockerImage,
dockerToken,
dockerUser,
name,
tags,
useCi,
} from './config';
type CodeBuildOptions = {
path:string;
spec?:string;
};
if (useCi) {
if (!dockerUser) {
throw `Pulumi config variable "docker_user" is required for CI.`;
}
if (!dockerToken) {
throw `Pulumi config variable "docker_token" is required for CI.`;
}
const repository = new aws.codecommit.Repository('git-repository', {
tags,
repositoryName: name,
defaultBranch: 'main',
});
const dockerHubToken = new aws.ssm.Parameter(`docker-hub-token`, {
type: 'String',
value: dockerToken || '',
});
const buildRole = new aws.iam.Role(`codecommit-role`, {
tags,
name: `${name}-ci`,
assumeRolePolicy: {
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Principal: {
Service: 'codebuild.amazonaws.com',
},
Action: 'sts:AssumeRole',
},
],
},
});
new aws.iam.RolePolicyAttachment(`codecommit-policy-1`, {
role: buildRole,
policyArn: 'arn:aws:iam::aws:policy/AdministratorAccess',
});
const createBuildServer = (project:string, options?:CodeBuildOptions) => {
const { path, spec } = options || {};
const buildProject =
new aws.codebuild.Project(`codebuild-project`, {
tags,
name: `${name}-${project}`,
serviceRole: buildRole.arn,
sourceVersion: `main`,
source: {
type: 'CODECOMMIT',
location: repository.cloneUrlHttp,
gitCloneDepth: 1,
buildspec: `${path}/${spec || 'buildspec.yml'}`,
},
environment: {
computeType: 'BUILD_GENERAL1_SMALL',
image: 'aws/codebuild/standard:5.0',
imagePullCredentialsType: 'CODEBUILD',
type: 'LINUX_CONTAINER',
privilegedMode: true,
environmentVariables: [
{
name: 'DOCKER_IMAGE',
value: dockerImage,
},
{
name: 'DOCKER_USER',
value: dockerUser || '',
},
{
type: 'PARAMETER_STORE',
name: 'DOCKER_TOKEN',
value: dockerHubToken.name,
},
],
},
artifacts: {
type: 'NO_ARTIFACTS',
},
});
return buildProject;
};
createBuildServer('application', { path: 'application' });
}