-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (75 loc) · 1.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
const core = require("@actions/core");
const fs = require("fs");
const AWS = require("aws-sdk");
const upload = async ({ awsConfig, file, bucket, contentType, distributionId, dest }) => {
try {
const s3 = new AWS.S3({ apiVersion: "2006-03-01", ...awsConfig });
const cloudFront = new AWS.CloudFront({
apiVersion: "2019-03-26",
...awsConfig
});
console.log("Uploading file to S3");
await s3
.upload({
Body: fs.readFileSync(file),
Bucket: bucket,
Key: dest,
ContentType: contentType
})
.promise();
if (!distributionId) {
return;
}
console.log("Invalidating CloudFront distribution");
await cloudFront
.createInvalidation({
DistributionId: distributionId,
InvalidationBatch: {
CallerReference: `${+new Date()}`,
Paths: {
Quantity: 1,
Items: ["/" + dest]
}
}
})
.promise();
} catch (err) {
throw err;
}
};
try {
const file = core.getInput("FILE");
const dest = core.getInput("DEST");
const bucket = core.getInput("AWS_S3_BUCKET");
const secretAccessKey = core.getInput("AWS_SECRET_KEY");
const accessKeyId = core.getInput("AWS_SECRET_ID");
const region = core.getInput("AWS_REGION");
const distributionId = core.getInput("AWS_DISTRIBUTION_ID");
const contentType = core.getInput("CONTENT_TYPE")
if (
!file ||
!dest ||
!bucket ||
!secretAccessKey ||
!accessKeyId ||
!region
) {
throw new Error("Not all inputs provided!");
}
upload({
awsConfig: {
accessKeyId,
secretAccessKey,
region
},
file,
bucket,
dest,
contentType,
distributionId
})
.then(() => console.log("Successfully uploaded file"))
.catch(err => core.setFailed(err.message));
} catch (error) {
core.setFailed(error.message);
}