This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (55 loc) · 1.76 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
const fs = require("fs");
const yaml = require("js-yaml");
const core = require("@actions/core");
const github = require("@actions/github");
const {
CodeBuildClient,
BatchGetProjectsCommand,
} = require("@aws-sdk/client-codebuild");
const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3");
async function main() {
if ("AWS_DEFAULT_REGION" in process.env && !("AWS_REGION" in process.env)) {
process.env.AWS_REGION = process.env.AWS_DEFAULT_REGION;
}
const codebuild = new CodeBuildClient();
let command = new BatchGetProjectsCommand({
names: [core.getInput("appname", { required: true })],
});
const data = await codebuild.send(command);
core.info("Uploading artifacts");
const artifactsBucket = data.projects[0].artifacts.location;
const files = yaml.load(data.projects[0].source.buildspec).artifacts.files;
const prefix = `external-${github.context.runNumber}/`;
core.setOutput("artifacts_bucket", artifactsBucket);
core.setOutput("artifacts_prefix", prefix);
const s3 = new S3Client();
files.forEach((file) => {
fs.readFile(`./${file}`, "utf8", function (err, contents) {
if (err) {
if (err.code === "ENOENT") {
core.warning(
`${file} does not exist. It must be uploaded to S3 for a deploy to succeed.`
);
} else {
raise(err);
}
} else {
command = new PutObjectCommand({
Bucket: artifactsBucket,
Key: `${prefix}${file}`,
Body: contents,
});
core.info(` * ${file}`);
s3.send(command).catch((error) => {
core.setFailed(error.message);
});
}
});
});
core.endGroup();
}
if (require.main === module) {
main().catch((error) => {
core.setFailed(error.message);
});
}