-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
77 lines (69 loc) · 2.56 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
const core = require("@actions/core");
const ssm = require("./ssm-helper");
const execSync = require("child_process").execSync;
async function run_action() {
const ssmPath = core.getInput("ssm-path", { required: true });
const ssmValue = core.getInput("ssm-value");
const ssmType = core.getInput("ssm-value-type");
const prefix = core.getInput("prefix");
const decryption = core.getInput("decryption") === "true";
const nullable = core.getInput("nullable") === "true";
const jsonAsString = core.getInput("json-as-string") === "true";
const region = process.env.AWS_DEFAULT_REGION;
try {
const paramValue = await ssm.getParameter(ssmPath, decryption, region);
jsonOrString(paramValue, core, prefix, ssmPath, jsonAsString);
} catch (error) {
core.debug(`Error name: ${error.name}`);
if (error.name === "ParameterNotFound") {
core.debug(`could not find parameter, attemping to create parameter`);
ssm.createSsmValue(ssmPath, region, ssmValue, ssmType, core, nullable);
jsonOrString(ssmValue, core, prefix, ssmPath, jsonAsString);
return;
} else {
core.debug(`could not find parameter: ${error.message}`);
}
}
}
function parseValue(val) {
try {
return JSON.parse(val);
} catch (error) {
core.debug(
"JSON parse failed - assuming parameter is to be taken as a string literal"
);
return val;
}
}
function setEnvironmentVar(key, value) {
cmdString = `echo "${key}=${value}" >> $GITHUB_ENV`;
core.debug(`Running cmd: ${cmdString}`);
execSync(cmdString, { stdio: "inherit" });
}
function jsonOrString(paramValue, core, prefix, ssmPath, jsonAsString) {
const parsedValue = parseValue(paramValue);
if (jsonAsString) {
const envVarName = getEnvName(ssmPath, prefix);
core.debug(`Using prefix + end of ssmPath for env var name: ${envVarName}`);
return setEnvironmentVar(envVarName, paramValue);
}
if (typeof parsedValue === "object") {
core.debug(`parsedValue: ${JSON.stringify(parsedValue)}`);
// Assume basic JSON structure
for (const key in parsedValue) {
setEnvironmentVar(prefix + key, parsedValue[key]);
}
return;
} else {
core.debug(`parsedValue: ${parsedValue}`);
// Set environment variable with ssmPath name as the env variable
const envVarName = getEnvName(ssmPath, prefix);
core.debug(`Using prefix + end of ssmPath for env var name: ${envVarName}`);
return setEnvironmentVar(envVarName, parsedValue);
}
}
function getEnvName(path, prefix) {
const split = path.split("/");
return prefix + split[split.length - 1];
}
run_action();