-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
157 lines (135 loc) · 4.69 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import core from "@actions/core";
import exec from "@actions/exec";
import { parseCSV } from "@google-github-actions/actions-utils";
import { promises as fsp } from "fs";
const getValues = (input) => {
const values = [];
for (const line of input.split(/\r|\n/)) {
const pieces = parseCSV(line);
for (const piece of pieces) {
const [key, value] = piece.trim().split('=', 2);
values.push([key, value]);
}
}
return values;
};
const parseJsonList = (json) => {
let list;
if (typeof json === "string") {
try {
list = JSON.parse(json);
} catch (err) {
// Assume it's a single string.
list = [json];
}
} else {
list = json;
}
if (!Array.isArray(list)) {
return [];
}
return list.filter((f) => !!f);
};
const run = async () => {
try {
const release = core.getInput("release");
const namespace = core.getInput("namespace");
const chart = core.getInput("chart");
const chartVersion = core.getInput("chart-version");
const repository = core.getInput("repository");
const values = getValues(core.getInput("values"));
const valueFiles = parseJsonList(core.getInput("value-files"));
const secretsFiles = parseJsonList(core.getInput("secrets-files"));
const task = core.getInput("task");
const timeout = core.getInput("timeout");
const dryRun = core.getInput("dry-run");
const atomic = core.getInput("atomic") || true;
const image = core.getInput("image");
let imageFields = parseJsonList(core.getInput("image-fields"));
const tag = core.getInput("tag");
let tagFields = parseJsonList(core.getInput("tag-fields"));
core.debug(`param: release = "${release}"`);
core.debug(`param: namespace = "${namespace}"`);
core.debug(`param: chart = "${chart}"`);
core.debug(`param: chartVersion = "${chartVersion}"`);
core.debug(`param: repository = "${repository}"`);
core.debug(`param: values = "${values}"`);
core.debug(`param: valueFiles = "${JSON.stringify(valueFiles)}"`);
core.debug(`param: secretsFiles = "${JSON.stringify(secretsFiles)}"`);
core.debug(`param: task = "${task}"`);
core.debug(`param: timeout = ${timeout}`);
core.debug(`param: dryRun = "${dryRun}"`);
core.debug(`param: atomic = "${atomic}"`);
core.debug(`param: image = "${image}"`);
core.debug(`param: imageFields = "${JSON.stringify(imageFields)}"`)
core.debug(`param: tag = "${tag}"`);
core.debug(`param: tagFields = "${JSON.stringify(tagFields)}"`)
const args = [
"upgrade",
release,
chart,
"--install",
"--wait",
`--namespace=${namespace}`,
"--dependency-update"
];
// Per https://helm.sh/docs/faq/#xdg-base-directory-support
process.env.XDG_DATA_HOME = "/root/.local/share";
process.env.XDG_CACHE_HOME = "/root/.cache";
process.env.XDG_CONFIG_HOME = "/root/.config";
// Set default image field to "image.name"
if (imageFields.length === 0) imageFields = ["image.name"]
// Set default tag field to "image.tag"
if (tagFields.length === 0) tagFields = ["image.tag"]
if (values) {
for (const [key, value] of values) {
args.push(`--set=${key}=${value}`);
}
}
if (image) {
for (const imageField of imageFields) {
args.push(`--set=${imageField}=${image}`);
}
}
if (tag) {
for (const tagField of tagFields) {
args.push(`--set=${tagField}=${tag}`);
}
}
if (dryRun) args.push("--dry-run");
if (chartVersion) args.push(`--version=${chartVersion}`);
if (repository) args.push(`--repo=${repository}`);
if (timeout) args.push(`--timeout=${timeout}`);
if (atomic === true) args.push("--atomic");
// Add all the value files
valueFiles.forEach((f) => args.push(`--values=${f}`));
// Add all the Helm Secrets files
secretsFiles.forEach((f) => args.push(`--values=secrets://${f}`));
// Setup the Kubeconfig file
if (process.env.KUBECONFIG_FILE) {
process.env.KUBECONFIG = "/kubeconfig.yml";
await fsp.writeFile(process.env.KUBECONFIG, process.env.KUBECONFIG_FILE);
}
// Setup the GCP credentials, if specified
if (process.env.GCP_KMS_KEY_FILE) {
process.env.GOOGLE_APPLICATION_CREDENTIALS = "/gcp_kms_key.json";
await fsp.writeFile(
process.env.GOOGLE_APPLICATION_CREDENTIALS,
process.env.GCP_KMS_KEY_FILE
);
}
if (task === "remove") {
// Delete the deployment
await exec.exec("helm", ["delete", "-n", namespace, release], {
ignoreReturnCode: true,
});
} else {
// Execute the deployment
await exec.exec("helm", args);
}
} catch (err) {
core.error(err);
core.setFailed(err.message);
}
};
run();