Skip to content

Commit caab363

Browse files
authored
Merge pull request #3 from vimeda/run-multiple-commands
First add repo, then run deployment
2 parents 6924066 + eef5203 commit caab363

1 file changed

Lines changed: 147 additions & 129 deletions

File tree

index.js

Lines changed: 147 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -145,150 +145,168 @@ function deleteCmd(helm, namespace, release) {
145145
return ["delete", "--purge", release];
146146
}
147147

148-
/**
149-
* Run executes the helm deployment.
148+
/*
149+
* Optionally add a helm repository
150150
*/
151-
async function run() {
152-
try {
153-
const context = github.context;
154-
await status("pending");
151+
async function addRepo(helm) {
152+
const repo = getInput("repo");
153+
const repoAlias = getInput("repo-alias");
154+
const repoUsername = getInput("repo-username");
155+
const repoPassword = getInput("repo-password");
155156

156-
const track = getInput("track") || "stable";
157-
const appName = getInput("release", required);
158-
const release = releaseName(appName, track);
159-
const namespace = getInput("namespace", required);
160-
const chart = chartName(getInput("chart", required));
161-
const chartVersion = getInput("chart_version");
162-
const values = getValues(getInput("values"));
163-
const task = getInput("task");
164-
const version = getInput("version");
165-
const valueFiles = getValueFiles(getInput("value_files"));
166-
const removeCanary = getInput("remove_canary");
167-
const helm = getInput("helm") || "helm";
168-
const timeout = getInput("timeout");
169-
const repo = getInput("repo");
170-
const repoAlias = getInput("repo-alias");
171-
const repoUsername = getInput("repo-username");
172-
const repoPassword = getInput("repo-password");
173-
const dryRun = core.getInput("dry-run");
174-
const secrets = getSecrets(core.getInput("secrets"));
175-
const atomic = getInput("atomic") || true;
176-
177-
core.debug(`param: track = "${track}"`);
178-
core.debug(`param: release = "${release}"`);
179-
core.debug(`param: appName = "${appName}"`);
180-
core.debug(`param: namespace = "${namespace}"`);
181-
core.debug(`param: chart = "${chart}"`);
182-
core.debug(`param: chart_version = "${chartVersion}"`);
183-
core.debug(`param: values = "${values}"`);
184-
core.debug(`param: dryRun = "${dryRun}"`);
185-
core.debug(`param: task = "${task}"`);
186-
core.debug(`param: version = "${version}"`);
187-
core.debug(`param: secrets = "${JSON.stringify(secrets)}"`);
188-
core.debug(`param: valueFiles = "${JSON.stringify(valueFiles)}"`);
189-
core.debug(`param: removeCanary = ${removeCanary}`);
190-
core.debug(`param: timeout = "${timeout}"`);
191-
core.debug(`param: atomic = "${atomic}"`);
192-
core.debug(`param: repo = "${repo}"`);
193-
core.debug(`param: repoAlias = "${repoAlias}"`);
194-
core.debug(`param: repoUsername = "${repoUsername}"`);
195-
core.debug(`param: repoPassword = "${repoPassword}"`);
196-
197-
// Setup command options and arguments.
198-
let args = [
199-
"upgrade",
200-
release,
201-
chart,
202-
"--install",
203-
"--wait",
204-
`--namespace=${namespace}`,
205-
];
206-
207-
// Per https://helm.sh/docs/faq/#xdg-base-directory-support
208-
if (helm === "helm3") {
209-
process.env.XDG_DATA_HOME = "/root/.helm/"
210-
process.env.XDG_CACHE_HOME = "/root/.helm/"
211-
process.env.XDG_CONFIG_HOME = "/root/.helm/"
212-
} else {
213-
process.env.HELM_HOME = "/root/.helm/"
214-
}
157+
core.debug(`param: repo = "${repo}"`);
158+
core.debug(`param: repoAlias = "${repoAlias}"`);
159+
core.debug(`param: repoUsername = "${repoUsername}"`);
160+
core.debug(`param: repoPassword = "${repoPassword}"`);
215161

216-
if (dryRun) args.push("--dry-run");
217-
if (appName) args.push(`--set=app.name=${appName}`);
218-
if (version) args.push(`--set=app.version=${version}`);
219-
if (chartVersion) args.push(`--version=${chartVersion}`);
220-
if (timeout) args.push(`--timeout=${timeout}`);
221-
valueFiles.forEach(f => args.push(`--values=${f}`));
222-
args.push("--values=./values.yml");
223-
224-
// Special behaviour is triggered if the track is labelled 'canary'. The
225-
// service and ingress resources are disabled. Access to the canary
226-
// deployments can be routed via the main stable service resource.
227-
if (track === "canary") {
228-
args.push("--set=service.enabled=false", "--set=ingress.enabled=false");
162+
if (repo !== "") {
163+
if (repoAlias === "") {
164+
throw new Error("repo alias is required when you are setting a repository");
229165
}
230166

231-
// If true upgrade process rolls back changes made in case of failed upgrade.
232-
if (atomic === true) {
233-
args.push("--atomic");
234-
}
167+
core.debug(`adding custom repository ${repo} with alias ${repoAlias}`);
235168

236-
// Setup necessary files.
237-
if (process.env.KUBECONFIG_FILE) {
238-
process.env.KUBECONFIG = "./kubeconfig.yml";
239-
await writeFile(process.env.KUBECONFIG, process.env.KUBECONFIG_FILE);
240-
}
241-
await writeFile("./values.yml", values);
169+
const args = [
170+
"repo",
171+
"add",
172+
repoAlias,
173+
repo,
174+
]
175+
176+
if (repoUsername) args.push(`--username=${repoUsername}`);
177+
if (repoPassword) args.push(`--password=${repoPassword}`);
178+
179+
return exec.exec(helm, args);
180+
}
181+
182+
return Promise.resolve()
183+
}
184+
185+
/*
186+
* Deploy the release
187+
*/
188+
async function deploy(helm) {
189+
const context = github.context;
190+
191+
const track = getInput("track") || "stable";
192+
const appName = getInput("release", required);
193+
const release = releaseName(appName, track);
194+
const namespace = getInput("namespace", required);
195+
const chart = chartName(getInput("chart", required));
196+
const chartVersion = getInput("chart_version");
197+
const values = getValues(getInput("values"));
198+
const task = getInput("task");
199+
const version = getInput("version");
200+
const valueFiles = getValueFiles(getInput("value_files"));
201+
const removeCanary = getInput("remove_canary");
202+
const timeout = getInput("timeout");
203+
const dryRun = core.getInput("dry-run");
204+
const secrets = getSecrets(core.getInput("secrets"));
205+
const atomic = getInput("atomic") || true;
206+
207+
core.debug(`param: track = "${track}"`);
208+
core.debug(`param: release = "${release}"`);
209+
core.debug(`param: appName = "${appName}"`);
210+
core.debug(`param: namespace = "${namespace}"`);
211+
core.debug(`param: chart = "${chart}"`);
212+
core.debug(`param: chart_version = "${chartVersion}"`);
213+
core.debug(`param: values = "${values}"`);
214+
core.debug(`param: dryRun = "${dryRun}"`);
215+
core.debug(`param: task = "${task}"`);
216+
core.debug(`param: version = "${version}"`);
217+
core.debug(`param: secrets = "${JSON.stringify(secrets)}"`);
218+
core.debug(`param: valueFiles = "${JSON.stringify(valueFiles)}"`);
219+
core.debug(`param: removeCanary = ${removeCanary}`);
220+
core.debug(`param: timeout = "${timeout}"`);
221+
core.debug(`param: atomic = "${atomic}"`);
222+
223+
// Setup command options and arguments.
224+
let args = [
225+
"upgrade",
226+
release,
227+
chart,
228+
"--install",
229+
"--wait",
230+
`--namespace=${namespace}`,
231+
];
232+
233+
process.env.XDG_DATA_HOME = "/root/.helm/"
234+
process.env.XDG_CACHE_HOME = "/root/.helm/"
235+
process.env.XDG_CONFIG_HOME = "/root/.helm/"
236+
237+
if (dryRun) args.push("--dry-run");
238+
if (appName) args.push(`--set=app.name=${appName}`);
239+
if (version) args.push(`--set=app.version=${version}`);
240+
if (chartVersion) args.push(`--version=${chartVersion}`);
241+
if (timeout) args.push(`--timeout=${timeout}`);
242+
243+
valueFiles.forEach(f => args.push(`--values=${f}`));
244+
245+
args.push("--values=./values.yml");
242246

243-
core.debug(`env: KUBECONFIG="${process.env.KUBECONFIG}"`);
247+
// Special behaviour is triggered if the track is labelled 'canary'. The
248+
// service and ingress resources are disabled. Access to the canary
249+
// deployments can be routed via the main stable service resource.
250+
if (track === "canary") {
251+
args.push("--set=service.enabled=false", "--set=ingress.enabled=false");
252+
}
253+
254+
// If true upgrade process rolls back changes made in case of failed upgrade.
255+
if (atomic === true) {
256+
args.push("--atomic");
257+
}
258+
259+
// Setup necessary files.
260+
if (process.env.KUBECONFIG_FILE) {
261+
process.env.KUBECONFIG = "./kubeconfig.yml";
262+
await writeFile(process.env.KUBECONFIG, process.env.KUBECONFIG_FILE);
263+
}
264+
265+
await writeFile("./values.yml", values);
244266

245-
// Render value files using github variables.
246-
await renderFiles(valueFiles.concat(["./values.yml"]), {
247-
secrets,
248-
deployment: context.payload.deployment,
267+
core.debug(`env: KUBECONFIG="${process.env.KUBECONFIG}"`);
268+
269+
// Render value files using github variables.
270+
await renderFiles(valueFiles.concat(["./values.yml"]), {
271+
secrets,
272+
deployment: context.payload.deployment,
273+
});
274+
275+
// Remove the canary deployment before continuing.
276+
if (removeCanary) {
277+
core.debug(`removing canary ${appName}-canary`);
278+
await exec.exec(helm, deleteCmd(helm, namespace, `${appName}-canary`), {
279+
ignoreReturnCode: true
249280
});
281+
}
250282

251-
// Remove the canary deployment before continuing.
252-
if (removeCanary) {
253-
core.debug(`removing canary ${appName}-canary`);
254-
await exec.exec(helm, deleteCmd(helm, namespace, `${appName}-canary`), {
255-
ignoreReturnCode: true
256-
});
257-
}
283+
// Actually execute the deployment here.
284+
if (task === "remove") {
285+
return exec.exec(helm, deleteCmd(helm, namespace, release), {
286+
ignoreReturnCode: true
287+
});
288+
}
258289

259-
if (repo !== "") {
260-
if (repoAlias === "") {
261-
core.setFailed("repo alias is required when you are setting a repository");
262-
await status("failure");
263-
}
290+
return exec.exec(helm, args);
291+
}
264292

265-
core.debug(`adding custom repository ${repo} with alias ${repoAlias}`);
266-
const repoAddArgs = [
267-
"repo",
268-
"add",
269-
repoAlias,
270-
repo,
271-
]
272-
273-
if (repoUsername) repoAddArgs.push(`--username=${repoUsername}`);
274-
if (repoPassword) repoAddArgs.push(`--password=${repoPassword}`);
275-
276-
repoAddArgs.push(";");
277-
repoAddArgs.push(helm);
278-
279-
args = repoAddArgs.concat(args)
280-
}
293+
/**
294+
* Run executes the helm deployment.
295+
*/
296+
async function run() {
297+
const commands = [addRepo, deploy]
298+
299+
try {
300+
await status("pending");
301+
302+
const helm = getInput("helm") || "helm3";
303+
core.debug(`param: helm = "${helm}"`);
281304

282-
// Actually execute the deployment here.
283-
if (task === "remove") {
284-
await exec.exec(helm, deleteCmd(helm, namespace, release), {
285-
ignoreReturnCode: true
286-
});
287-
} else {
288-
await exec.exec(helm, args);
305+
for(const command of commands) {
306+
await command(helm);
289307
}
290308

291-
await status(task === "remove" ? "inactive" : "success");
309+
await status("success");
292310
} catch (error) {
293311
core.error(error);
294312
core.setFailed(error.message);

0 commit comments

Comments
 (0)