Skip to content

Commit 4b30064

Browse files
committed
feat: Templating of value files
Allow a value file list and allow templating.
1 parent abc7b15 commit 4b30064

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

index.js

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ const github = require("@actions/github");
33
const exec = require("@actions/exec");
44
const fs = require("fs");
55
const util = require("util");
6+
const Mustache = require("mustache");
67

78
const writeFile = util.promisify(fs.writeFile);
9+
const readFile = util.promisify(fs.readFile);
810
const required = { required: true };
911

1012
/**
@@ -62,6 +64,33 @@ function getValues(values) {
6264
return values;
6365
}
6466

67+
function getSecrets(secrets) {
68+
if (typeof secrets === "string") {
69+
try {
70+
return JSON.stringify(secrets);
71+
} catch (err) {
72+
return secrets;
73+
}
74+
}
75+
return secrets;
76+
}
77+
78+
function getValueFiles(files) {
79+
let fileList;
80+
if (typeof files === "string") {
81+
try {
82+
fileList = JSON.parse(files);
83+
} catch (err) {
84+
// Assume it's a single string.
85+
fileList = [files];
86+
}
87+
}
88+
if (!Array.isArray(fileList)) {
89+
return [];
90+
}
91+
return fileList;
92+
}
93+
6594
function getInput(name, options) {
6695
const context = github.context;
6796
const deployment = context.payload.deployment;
@@ -79,23 +108,43 @@ function getInput(name, options) {
79108
return val;
80109
}
81110

82-
function render() {}
111+
/**
112+
* Render files renders data into the list of provided files.
113+
* @param {Array<string>} files
114+
* @param {any} data
115+
*/
116+
function renderFiles(files, data) {
117+
core.debug(
118+
`rendering value files [${files.join(",")}] with: ${JSON.stringify(data)}`
119+
);
120+
const tags = ["${{", "}}"];
121+
const promises = files.map(async file => {
122+
const content = await readFile(file, { encoding: "utf8" });
123+
const rendered = Mustache.render(content, data, {}, tags);
124+
await writeFile(file, rendered);
125+
});
126+
return Promise.all(promises);
127+
}
83128

84129
/**
85130
* Run executes the helm deployment.
86131
*/
87132
async function run() {
88133
try {
134+
const context = github.context;
89135
await status("pending");
90136

91137
const track = getInput("track") || "stable";
92138
const release = releaseName(getInput("release", required), track);
93139
const namespace = getInput("namespace", required);
94140
const chart = chartName(getInput("chart", required));
95141
const values = getValues(getInput("values"));
96-
const dryRun = getInput("dry_run");
97142
const task = getInput("task");
98143
const version = getInput("version");
144+
const valueFiles = getValueFiles(getInput("value_files"));
145+
146+
const dryRun = core.getInput("dry-run");
147+
const secrets = getSecrets(core.getInput("secrets"));
99148

100149
core.debug(`param: track = "${track}"`);
101150
core.debug(`param: release = "${release}"`);
@@ -105,6 +154,8 @@ async function run() {
105154
core.debug(`param: dryRun = "${dryRun}"`);
106155
core.debug(`param: task = "${task}"`);
107156
core.debug(`param: version = "${version}"`);
157+
core.debug(`param: secrets = "${secrets}"`);
158+
core.debug(`param: valueFiles = "${valueFiles}"`);
108159

109160
// Setup command options and arguments.
110161
const opts = { env: {} };
@@ -136,6 +187,12 @@ async function run() {
136187

137188
core.debug(`env: KUBECONFIG="${opts.env.KUBECONFIG}"`);
138189

190+
// Render value files using github variables.
191+
await renderFiles(valueFiles.concat(["./values.yml"]), {
192+
secrets,
193+
deployment: context.payload.deployment,
194+
});
195+
139196
// Actually execute the deployment here.
140197
if (task === "remove") {
141198
await exec.exec("helm", ["delete", release, "--purge"], {

0 commit comments

Comments
 (0)