generated from cloudposse-github-actions/composite-template
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
91 lines (71 loc) · 2.59 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
79
80
81
82
83
84
85
86
87
88
89
90
91
const core = require('@actions/core');
const yaml = require('yaml')
const {DefaultArtifactClient} = require('@actions/artifact')
const crypto = require('crypto');
const fs = require('fs');
try {
const step_name = core.getInput('matrix-step-name');
const matrix_key = core.getInput('matrix-key');
const outputs = core.getInput('outputs');
core.debug("step_name:")
core.debug(step_name)
core.debug("matrix_key:")
core.debug(matrix_key)
core.debug("outputs:")
core.debug(outputs)
function isEmptyInput(value) {
return value === null || value.trim() === "";
}
if (isEmptyInput(step_name) && !isEmptyInput(matrix_key)) {
core.setFailed("`matrix-step-name` can not be empty when `matrix-key` specified");
return
}
if (!isEmptyInput(step_name) && isEmptyInput(matrix_key)) {
core.setFailed("`matrix-key` can not be empty when `matrix-step-name` specified");
return
}
const matrix_mode = !isEmptyInput(step_name) && !isEmptyInput(matrix_key)
if (!isEmptyInput(outputs)) {
try {
yaml.parse(outputs)
}
catch (error) {
message = `Outputs should be valid YAML
---------------------
${outputs}
---------------------
${error}`;
core.setFailed(message);
return
}
}
const outputs_struct = !isEmptyInput(outputs) ? yaml.parse(outputs) : {}
Object.keys(outputs_struct).forEach(function(key, index) {
core.setOutput(key, outputs_struct[key])
});
core.debug("outputs_struct:")
core.debug(outputs_struct)
core.debug("JSON.stringify(outputs_struct):")
core.debug(JSON.stringify(outputs_struct))
core.setOutput('result', JSON.stringify(outputs_struct))
if (!isEmptyInput(outputs) && matrix_mode) {
const artifact_content = isEmptyInput(matrix_key) ? outputs_struct : { [matrix_key]: outputs_struct }
fs.writeFileSync("./" + step_name, JSON.stringify(artifact_content));
const fileBuffer = fs.readFileSync("./" + step_name);
const hashSum = crypto.createHash('sha256');
hashSum.update(fileBuffer);
const hex = hashSum.digest('hex');
const artifactClient = new DefaultArtifactClient();
const artifactName = hex;
const files = [
"./" + step_name,
]
const rootDirectory = '.' // Also possible to use __dirname
const options = {
continueOnError: false
}
artifactClient.uploadArtifact(artifactName, files, rootDirectory, options)
}
} catch (error) {
core.setFailed(error.message);
}