This repository has been archived by the owner on May 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
45 lines (37 loc) · 1.53 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
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const fs = require("fs");
exports.handler = (event, context, callback) => {
const bucket = process.env.BUCKET;
if (bucket && bucket !== '') {
console.log(`Uploading badges to ${bucket}.`);
const isPipeline = typeof event.detail.pipeline !== "undefined";
const project = isPipeline ? event.detail.pipeline : event.detail['project-name'];
const status = isPipeline ? event.detail.state : event.detail['build-status'];
const image = `badges/${isPipeline ? 'pipeline' : 'build'}-${status}.svg`;
const key = `${project}.svg`;
console.log(`Build for project ${project} was a ${status} so uploading` +
` the image ${image} to s3://${bucket}/${key}`);
const params = {
Bucket: bucket,
Key: key,
Body: fs.readFileSync(image),
ACL: 'public-read',
ContentType: 'image/svg+xml'
};
s3.putObject(params, (err, data) => {
if (err) {
const message = `Failed to upload image to S3: ${err}`;
console.error(message);
callback(message, null);
} else {
const message = 'Upload complete!';
console.log(message);
callback(null, message);
}
});
} else {
callback('You must set the "BUCKET" environment variable to the name' +
'of the bucket that your badges will be uploaded to.', null);
}
};