/
app.js
62 lines (51 loc) · 1.94 KB
/
app.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
'use strict';
const aws = require('aws-sdk');
const codedeploy = new aws.CodeDeploy({ apiVersion: '2014-10-06' });
const util = require('util');
const exec = util.promisify(require('child_process').exec);
exports.handler = async function (event, context, callback) {
const postmanApiKey = process.env.POSTMAN_API_KEY || '';
const postmanCollectionUid = process.env.POSTMAN_COLLECTION_UID || '';
//Read the DeploymentId from the event payload.
const deploymentId = event.DeploymentId;
//Read the LifecycleEventHookExecutionId from the event payload
const lifecycleEventHookExecutionId = event.LifecycleEventHookExecutionId;
// Prepare the validation test results with the deploymentId and
// the lifecycleEventHookExecutionId for CodeDeploy.
let params = {
deploymentId: deploymentId,
lifecycleEventHookExecutionId: lifecycleEventHookExecutionId,
};
const postmanCommand = `newman run https://api.getpostman.com/collections/${postmanCollectionUid}?apikey=${postmanApiKey}`;
console.log(`newman command: ${postmanCommand}`);
try {
const { stdout, stderr } = await exec(postmanCommand);
if (stderr) {
params.status = 'Failed';
console.log(`newman test error result: ${stderr}`);
} else {
params.status = 'Succeeded';
console.log(`newman test result: ${stdout}`);
}
} catch (error) {
params.status = 'Failed';
console.log(`errorMessage: ${error.errorMessage}`);
console.log(`newman test error result: ${error.stdout}`);
}
console.log(`params: ${JSON.stringify(params)}`);
// Pass CodeDeploy the prepared validation test results.
const promise = new Promise(function (resolve, reject) {
codedeploy.putLifecycleEventHookExecutionStatus(
params,
function (err, data) {
if (err) {
reject(Error(err));
} else {
// Validation succeeded.
resolve('Validation test succeeded');
}
}
);
});
return promise;
};