Skip to content

Commit

Permalink
feat: Templating of value files
Browse files Browse the repository at this point in the history
Allow a value file list and allow templating.
  • Loading branch information
colinjfw committed Sep 1, 2019
1 parent abc7b15 commit 4b30064
Showing 1 changed file with 59 additions and 2 deletions.
61 changes: 59 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ const github = require("@actions/github");
const exec = require("@actions/exec");
const fs = require("fs");
const util = require("util");
const Mustache = require("mustache");

const writeFile = util.promisify(fs.writeFile);
const readFile = util.promisify(fs.readFile);
const required = { required: true };

/**
Expand Down Expand Up @@ -62,6 +64,33 @@ function getValues(values) {
return values;
}

function getSecrets(secrets) {
if (typeof secrets === "string") {
try {
return JSON.stringify(secrets);
} catch (err) {
return secrets;
}
}
return secrets;
}

function getValueFiles(files) {
let fileList;
if (typeof files === "string") {
try {
fileList = JSON.parse(files);
} catch (err) {
// Assume it's a single string.
fileList = [files];
}
}
if (!Array.isArray(fileList)) {
return [];
}
return fileList;
}

function getInput(name, options) {
const context = github.context;
const deployment = context.payload.deployment;
Expand All @@ -79,23 +108,43 @@ function getInput(name, options) {
return val;
}

function render() {}
/**
* Render files renders data into the list of provided files.
* @param {Array<string>} files
* @param {any} data
*/
function renderFiles(files, data) {
core.debug(
`rendering value files [${files.join(",")}] with: ${JSON.stringify(data)}`
);
const tags = ["${{", "}}"];
const promises = files.map(async file => {
const content = await readFile(file, { encoding: "utf8" });
const rendered = Mustache.render(content, data, {}, tags);
await writeFile(file, rendered);
});
return Promise.all(promises);
}

/**
* Run executes the helm deployment.
*/
async function run() {
try {
const context = github.context;
await status("pending");

const track = getInput("track") || "stable";
const release = releaseName(getInput("release", required), track);
const namespace = getInput("namespace", required);
const chart = chartName(getInput("chart", required));
const values = getValues(getInput("values"));
const dryRun = getInput("dry_run");
const task = getInput("task");
const version = getInput("version");
const valueFiles = getValueFiles(getInput("value_files"));

const dryRun = core.getInput("dry-run");
const secrets = getSecrets(core.getInput("secrets"));

core.debug(`param: track = "${track}"`);
core.debug(`param: release = "${release}"`);
Expand All @@ -105,6 +154,8 @@ async function run() {
core.debug(`param: dryRun = "${dryRun}"`);
core.debug(`param: task = "${task}"`);
core.debug(`param: version = "${version}"`);
core.debug(`param: secrets = "${secrets}"`);
core.debug(`param: valueFiles = "${valueFiles}"`);

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

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

// Render value files using github variables.
await renderFiles(valueFiles.concat(["./values.yml"]), {
secrets,
deployment: context.payload.deployment,
});

// Actually execute the deployment here.
if (task === "remove") {
await exec.exec("helm", ["delete", release, "--purge"], {
Expand Down

0 comments on commit 4b30064

Please sign in to comment.