-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
62 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,78 @@ | ||
const core = require('@actions/core') | ||
const ssm = require('./ssm-helper') | ||
const fs = require('fs') | ||
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' | ||
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) { | ||
const parsedValue = parseValue(param.Value) | ||
if (typeof(parsedValue) === 'object') { | ||
core.debug(`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 { | ||
core.debug(`parsedValue: ${parsedValue}`) | ||
// Set environment variable with ssmPath name as the env variable | ||
var split = param.Name.split('/') | ||
var envVarName = prefix + split[split.length - 1] | ||
core.debug(`Using prefix + end of ssmPath for env var name: ${envVarName}`) | ||
setEnvironmentVar(envVarName, parsedValue, maskValues) | ||
envs.push(`${envVarName}=${parsedValue}`) | ||
} | ||
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]}`); | ||
} | ||
writeEnvFile(out, envs) | ||
} catch (e) { | ||
core.setFailed(e.message) | ||
} 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')) | ||
} | ||
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 { | ||
core.debug('JSON parse failed - assuming parameter is to be taken as a string literal') | ||
return 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) | ||
if (maskValue) { | ||
core.setSecret(value); | ||
} | ||
core.exportVariable(key, value); | ||
} | ||
|
||
run_action() | ||
run_action(); |