-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
222 lines (198 loc) · 7.12 KB
/
index.js
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
'use strict';
const AWS = require('aws-sdk');
const fs = require('fs');
class ServerlessAwsCi {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.commands = {
awsci: {
usage: 'Creates a CI pipeline using AWS CodePipeline and AWS CodeBuild',
lifecycleEvents: [
'initialize',
'create',
],
options: {
repo: {
usage: 'Specify the full GitHub repo (owner/repo) of the source repository',
shortcut: 'g',
required: true,
},
branch: {
usage: 'Specify the GitHub repo branch to watch for commits',
shortcut: 'b',
required: true,
},
token: {
usage: 'Specify the GitHub personal access token (PAT) of your configured source repository',
shortcut: 't',
required: true,
},
},
},
};
this.hooks = {
'awsci:initialize': this.initializeVariables.bind(this),
'awsci:create': this.run.bind(this),
};
}
initializeVariables() {
// Sets the credentials for AWS resources.
const awsCreds = this.serverless.providers.aws.getCredentials();
AWS.config.update(awsCreds);
this.codeBuild = new AWS.CodeBuild();
this.codePipeline = new AWS.CodePipeline();
this.cloudFormation = new AWS.CloudFormation();
}
run() {
return Promise.all([this.createBuildProject(), this.createPipeline(), this.createBuildSpec()])
.then(() => (this.serverless.cli.log(`The pipeline was successfully created.\nYou must commit the created buildspec.yml file to the "${this.options.branch}" branch.`)))
.catch((err) => {
throw new Error(`The pipeline was not created: ${err}`);
});
}
/**
* Gets the deployment bucket
*/
getDeploymentBucket() {
if (this.serverless.service.provider.deploymentBucket) {
return Promise.resolve(this.serverless.service.provider.deploymentBucket).then(data => data);
}
const params = {
LogicalResourceId: 'ServerlessDeploymentBucket',
StackName: `${this.serverless.service.service}-${this.serverless.processedInput.options.stage || this.serverless.service.provider.stage}`,
};
return this.cloudFormation.describeStackResource(params).promise().then(data => data.StackResourceDetail.PhysicalResourceId).catch(() => { throw new Error('Cannot find AWS::S3::DeploymentBucket'); });
}
createPipeline() {
this.serverless.cli.log('Creating AWS CodePipeline pipeline...');
const service = this.serverless.service;
const [gitOwner, gitRepo] = this.options.repo.split('/');
if (!service.custom.awsCI || !service.custom.awsCI.roleArn) {
throw new Error('awsCI settings in Serverless are not configured correctly: roleArn missing');
}
return this.getDeploymentBucket().then((deploymentBucket) => {
const params = {
pipeline: {
version: 1,
name: `${this.serverless.service.service}-${this.serverless.processedInput.options.stage || this.serverless.service.provider.stage}`,
artifactStore: {
type: 'S3',
location: deploymentBucket,
},
roleArn: service.custom.awsCI.roleArn,
stages: [
{
name: 'Source',
actions: [
{
name: 'Source',
actionTypeId: {
version: '1',
category: 'Source',
owner: 'ThirdParty',
provider: 'GitHub',
},
configuration: {
Owner: gitOwner,
Repo: gitRepo,
Branch: this.options.branch,
OAuthToken: this.options.token,
},
inputArtifacts: [
],
outputArtifacts: [
{
name: 'ServerlessSource',
},
],
runOrder: 1,
},
],
},
{
name: 'Deploy',
actions: [
{
name: 'ServerlessDeploy',
actionTypeId: {
version: '1',
category: 'Build',
owner: 'AWS',
provider: 'CodeBuild',
},
configuration: {
ProjectName: `${this.serverless.service.service}-${this.serverless.processedInput.options.stage || this.serverless.service.provider.stage}`,
},
inputArtifacts: [
{
name: 'ServerlessSource',
},
],
outputArtifacts: [
],
runOrder: 1,
},
],
},
],
},
};
return this.codePipeline.createPipeline(params).promise();
});
}
createBuildProject() {
return this.findBuildProject().then((data) => {
if (data.projects.indexOf(`${this.serverless.service.service}-${this.serverless.processedInput.options.stage || this.serverless.service.provider.stage}`) !== -1) {
this.serverless.cli.log('Found existing AWS CodePBuild project');
return Promise.resolve();
}
this.serverless.cli.log('Creating AWS CodePBuild project...');
const service = this.serverless.service;
if (!service.custom.awsCI || !service.custom.awsCI.roleArn) {
throw new Error('awsCI settings in Serverless are not configured correctly: roleArn missing');
}
const params = {
artifacts: {
type: 'CODEPIPELINE',
},
environment: {
computeType: 'BUILD_GENERAL1_SMALL',
image: 'aws/codebuild/nodejs:7.0.0',
type: 'LINUX_CONTAINER',
},
name: `${this.serverless.service.service}-${this.serverless.processedInput.options.stage || this.serverless.service.provider.stage}`,
source: {
type: 'CODEPIPELINE',
},
serviceRole: service.custom.awsCI.roleArn,
};
return this.codeBuild.createProject(params).promise();
});
}
findBuildProject() {
return this.codeBuild.listProjects({}).promise();
}
/*
* Obtains the certification arn
*/
createBuildSpec() {
this.serverless.cli.log('Creating AWS CodePipeline buildspec.yml file...');
const service = this.serverless.service;
if (!service.custom.awsCI) {
throw new Error('awsCI settings in Serverless are not configured correctly');
}
const buildSpec = `version: 0.2
phases:
build:
commands:
- npm install -g serverless && npm install && serverless deploy --stage "${this.options.stage || this.serverless.service.provider.stage}" --region "${this.options.region || this.serverless.service.provider.region}"`;
return new Promise((resolve, reject) => {
fs.writeFile('buildspec.yml', buildSpec, (err) => {
if (err) reject(err);
else resolve(buildSpec);
});
});
}
}
module.exports = ServerlessAwsCi;