-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (72 loc) · 2.3 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
const core = require("@actions/core");
const ssm = require("./ssm-helper");
const fs = require("fs");
async function run_action() {
try {
const ssmPath = core.getInput("ssm-path", { required: true });
const getChildren = core.getInput("get-children") === "true";
const prefix = core.getInput("prefix");
const region = process.env.AWS_DEFAULT_REGION;
const decryption = core.getInput("decryption") === "true";
const maskValues = core.getInput("mask-values") === "true";
const out = core.getInput("out", { required: false }) || ".env";
const params = await ssm.getParameters(
ssmPath,
getChildren,
decryption,
region
);
const envs = [];
for (let param of params) {
console.log("param: ", param);
const parsedValue = parseValue(param.Value);
if (typeof parsedValue === "object") {
console.log(`parsedValue: ${JSON.stringify(parsedValue)}`);
// Assume basic JSON structure
for (var key in parsedValue) {
setEnvironmentVar(prefix + key, parsedValue[key], maskValues);
envs.push(`${prefix + key}=${parsedValue[key]}`);
}
} else {
console.log(`parsedValue: ${parsedValue}`);
// Set environment variable with ssmPath name as the env variable
var split = param.Name.split("/");
var envVarName = prefix + split[split.length - 1];
console.log(
`Using prefix + end of ssmPath for env var name: ${envVarName}`
);
setEnvironmentVar(envVarName, parsedValue, maskValues);
envs.push(`${envVarName}=${parsedValue}`);
}
}
writeEnvFile(out, envs);
} catch (e) {
core.setFailed(e.message);
}
}
function writeEnvFile(output, envs) {
if (fs.existsSync(output)) {
console.log(`append to ${output} file`);
fs.appendFileSync(output, "\n" + envs.join("\n"));
} else {
console.log(`create ${output} file`);
fs.writeFileSync(output, envs.join("\n"));
}
}
function parseValue(val) {
try {
return JSON.parse(val);
} catch {
console.log(
"JSON parse failed - assuming parameter is to be taken as a string literal"
);
return val;
}
}
function setEnvironmentVar(key, value, maskValue) {
if (maskValue) {
core.setSecret(value);
}
core.exportVariable(key, value);
}
run_action();