-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (65 loc) · 2.18 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
/**
* @author: jdrydn <james@jdrydn.com> (https://github.com/jdrydn)
* @license: MIT
* @link: https://github.com/someimportantcompany/github-actions-aws-s3-env
* @variation: 3c1b552680fe
*/
const core = require('@actions/core');
const dotenv = require('dotenv');
const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3');
function assert(value, err) {
if (!value) {
throw err;
}
}
async function getEnvFile(from) {
assert(typeof from === 'string', new TypeError('Expected from to be a string'));
assert(from.startsWith('s3://'), new TypeError('Expected from string to start with s3://'));
const Bucket = from.substring(5, from.indexOf('/', 5));
const Key = from.substring(from.indexOf('/', 5) + 1);
core.info(`Fetching env file from s3://${Bucket}/${Key}`);
const client = new S3Client({});
const command = new GetObjectCommand({
Bucket,
Key,
ResponseContentType: 'text/plain',
});
const res = await client.send(command);
const body = await res.Body.transformToString();
return body;
}
module.exports = async function run() {
try {
const from = core.getInput('from', { required: true });
const prefix = core.getInput('prefix');
const masked = core.getInput('masked').toString() === 'true';
const exportEnv = core.getInput('export-env').toString() !== 'false';
const exportOutputs = core.getInput('export-outputs').toString() === 'true';
const raw = await getEnvFile(from);
const env = dotenv.parse(raw);
core.debug(env);
const listOutput = exportOutputs ? [] : undefined;
for (const [key, value] of Object.entries(env)) {
if ((exportEnv || exportOutputs) && masked) {
core.setSecret(value);
}
if (exportEnv) {
core.exportVariable(`${prefix}${key}`, value);
core.info(`export ${prefix}${key}=${value}`);
}
if (exportOutputs) {
core.setOutput(`env.${prefix}${key}`, value);
listOutput.push(`${prefix}${key}=${value}`);
}
}
if (exportOutputs) {
core.setOutput('list', listOutput.join('\n'));
}
} catch (err) {
core.setFailed(err.message);
}
};
/* istanbul ignore next */
if (require.main === module) {
module.exports();
}