-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
65 lines (54 loc) · 1.83 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
const { join } = require('path');
const { readFileSync, mkdirSync, writeFileSync } = require('fs');
const core = require('@actions/core');
const artifact = require('@actions/artifact');
const rimraf = require('rimraf');
const WORKDIR = join(process.cwd(), '_persist_action_dir');
async function storeData(variable, data){
var client = artifact.create();
const file = join(WORKDIR, `${variable}.txt`);
// cleanup old directories if needed
rimraf.sync(WORKDIR);
mkdirSync(WORKDIR);
writeFileSync(file, data, { encoding: 'utf8' });
await client.uploadArtifact(variable, [file], process.cwd())
}
async function loadData(variables){
var client = artifact.create();
// cleanup old directories if needed
rimraf.sync(WORKDIR);
mkdirSync(WORKDIR);
for (const v of variables) {
let data;
try {
const file = join(WORKDIR, `${v}.txt`);
await client.downloadArtifact(v);
data = readFileSync(file, { encoding: 'utf8' }).toString();
} catch (error) {
core.warning(`Variable ${v} not found`)
}
core.setOutput(v, data);
core.exportVariable(v, data);
// store the same data with a fixed prefix so it can be iterated over if needed
core.exportVariable(`persist-action-data-${v}`, data);
}
}
async function runAction(){
const inputs = {
data: core.getInput('data'),
variable: core.getInput('variable'),
retrieve: core.getInput('retrieve_variables'),
}
if (inputs.retrieve) {
const vars = inputs.retrieve.split(',').map(v=>v.trim());
await loadData(vars)
return;
}
await storeData(inputs.variable, inputs.data)
}
runAction().then(() => {
core.info('Action completed successfully');
})
.catch(e => {
core.setFailed(e.toString());
});