-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
144 lines (125 loc) · 3.86 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
const aws = require('aws-sdk');
const cloudfront = new aws.CloudFront();
const codepipeline = new aws.CodePipeline();
/**
*
* @param {*} jobId
* @returns {Promise} Promise object that returns an empty body if resolved. Error object will be passed in first argument if rejected.
* {@link https://docs.aws.amazon.com/codepipeline/latest/APIReference/API_PutJobSuccessResult.html} for more information on errors.
*/
let codepipelineSuccess = (jobId) => {
return new Promise((resolve, reject) => {
codepipeline.putJobSuccessResult({ jobId }, (err, data) => {
if (err) {
reject(err);
return;
}
resolve();
})
});
}
/**
*
* @param {*} jobId
* @param {*} error
* @param {*} awsRequestId
* @returns {Promise} Promise object that returns an empty body if resolved. Error object will be passed in first argument if rejected.
* {@link https://docs.aws.amazon.com/codepipeline/latest/APIReference/API_PutJobFailureResult.html} for more information on errors.
*/
let codepipelineFail = (jobId, error, awsRequestId) => {
return new Promise((resolve, reject) => {
codepipeline.putJobFailureResult({
jobId,
failureDetails: {
message: JSON.stringify(error),
type: 'JobFailed',
externalExecutionId: awsRequestId
}
}, (err, data) => {
if (err) {
reject(err);
return;
}
resolve();
})
});
}
/**
*
* @param {*} distributionId
*
* @param {*} jobId
* @returns {Promise}
* {@link https://docs.aws.amazon.com/cloudfront/latest/APIReference/API_CreateInvalidation.html} for more information on createInvalidation implementation.
*/
let createInvalidation = (distributionId, jobId, items) => {
return new Promise((resolve, reject) => {
cloudfront.createInvalidation({
'DistributionId': distributionId,
'InvalidationBatch': {
'CallerReference': jobId,
'Paths': {
'Items': items,
'Quantity': items.length,
}
}
}, (err, data) => {
console.log(data);
if (err) {
reject(err);
return;
}
resolve(data);
});
});
}
/**
*
* @param {*} event
* @param {*} context
* @returns {Object} Returns an object with a status code
*/
exports.handler = async (event, context) => {
// Validate that we have the variables we need
if (!event['CodePipeline.job'] && !event['CodePipeline.job'].id) {
return {
status: 400,
error: {
message: 'CodePipeline Job ID not provided in the event argument.'
}
}
}
const jobId = event['CodePipeline.job'].id;
const invalidationConfigString = event["CodePipeline.job"].data.actionConfiguration.configuration.UserParameters;
const { distributionId, items } = JSON.parse(invalidationConfigString);
if (!distributionId) {
return {
status: 400,
error: {
message: 'User parameter "distributionId" not found.',
}
}
}
if(!items || items.length === 0) {
return {
status: 400,
error: {
message: 'User parameter "items" is missing or has no paths.',
}
}
}
try {
const invalidationResponse = await createInvalidation(distributionId, jobId, items);
await codepipelineSuccess(jobId);
return {
status: 200,
data: invalidationResponse
}
} catch (error) {
await codepipelineFail(jobId, error, context.awsRequestId);
return {
status: 400,
error: error
}
}
};