-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (52 loc) · 2.46 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
const core = require('@actions/core');
const exec = require('@actions/exec');
const os = require('os');
const fs = require('fs');
const path = require('path');
const commandExists = require('command-exists');
async function run() {
const kustomizeVersion = core.getInput('kustomize_version', {required: true});
const architecture = core.getInput('architecture', {required: true});
const kustomizationDirectory = path.join(process.env.GITHUB_WORKSPACE, core.getInput('kustomize_directory', {required: true}));
const outputPath = core.getInput('output_path', {required: true})
const kustomizePath = path.join(process.env.GITHUB_WORKSPACE, 'kustomize');
// Build Kustomize Configuration and process it with envsubst
// Save the result to a temporary file
const tempFile = path.join(os.tmpdir(), 'kustomize_output.yaml');
const kubectlInstalled = commandExists('kubectl', function (err, commandExists) {
return commandExists;
});
if (!kubectlInstalled) {
if (!fs.existsSync(kustomizePath)) {
// Download Kustomize
core.debug(`Downloading kustomize version ${kustomizeVersion} for ${architecture}`);
await exec.exec(`curl -sLO https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/${kustomizeVersion}/kustomize_${kustomizeVersion}_${architecture}.tar.gz`);
await exec.exec(`tar xzf ./kustomize_${kustomizeVersion}_${architecture}.tar.gz -C ${process.env.GITHUB_WORKSPACE}`);
}
core.debug(`Running command: \`${kustomizePath} build ${kustomizationDirectory} -o ${tempFile}\``);
await exec.exec(`${kustomizePath} build ${kustomizationDirectory} -o ${tempFile}`);
} else {
core.debug(`Running command: \`kubectl kustomize ${kustomizationDirectory} -o ${tempFile}\``);
await exec.exec(`kubectl kustomize ${kustomizationDirectory} -o ${tempFile}`);
}
await exec.exec(`bash -c "cat ${tempFile} | envsubst > ${outputPath}"`, function (error, stdout, stderr) {
if (error) {
core.setFailed(`Failed to process kustomize output with envsubst: ${error}`);
}
try {
fs.writeFile(outputPath, stdout);
} catch (error) {
core.setFailed(`Failed to write to ${outputPath}: ${error}`);
}
fs.unlink(tempFile);
})
core.debug(`Setting output 'result' to ${outputPath}`)
core.setOutput('result', outputPath);
// Clean up the temporary file
try {
fs.unlinkSync(tempFile);
} catch (error) {
core.warning(`Failed to delete temporary file ${tempFile}: ${error}`);
}
}
run();