Skip to content

Commit

Permalink
feat: Implement deployment status
Browse files Browse the repository at this point in the history
  • Loading branch information
colinjfw committed Aug 23, 2019
1 parent 595a429 commit 2069c0b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ inputs:
namespace:
description: 'Kubernetes namespace name'
chart:
description: 'Helm chart name'
description: 'Helm chart path'
values:
description: 'Helm chart values, expected to be a YAML or JSON string'
token:
description: >
Github repository token. If included and the event is a deployment then
the deployment_status event will be fired.
runs:
using: 'docker'
image: 'Dockerfile'
75 changes: 61 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,81 @@ const writeFile = util.promisify(fs.writeFile);

const required = { required: true };

/**
* Status marks the deployment status. Only activates if token is set as an
* input to the job.
*
* @param {string} state
*/
async function status(state) {
try {
const context = github.context;
const deployment = context.payload.deployment;
const token = core.getInput("token");
if (!token || !deployment) {
core.debug("Not setting deployment status")
return;
}

const client = new github.GitHub(token);
const url = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${context.sha}/checks`

await client.repos.createDeploymentStatus({
...context.repo,
deployment_id: deployment.id,
state,
log_url: url,
});
} catch (error) {
core.warning(`Failed to set deployment status: ${error.message}`);
}
}

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

const release = core.getInput("release", required);
const namespace = core.getInput("namespace", required);
const chart = core.getInput("chart", required);
const values = core.getInput("values");
const values = core.getInput("values") || "{}";

// Load in the github context and deployment event.
const context = github.context;
const deployment = context.payload.deployment;
const opts = {
env: {
KUBECONFIG: "./kubeconfig.yml",
}
}
const task = (context.payload.deployment &&
context.payload.deployment.task) || "deploy";

// Setup command options and arguments.
const opts = { env: {} };
const args = [
"upgrade", release, chart,
"--install", "--wait", "--atomic",
"--namespace", namespace,
"--values", "./values.yml",
];

await writeFile("./kubeconfig.yml", process.env.KUBECONFIG);
// Setup necessary files.
if (process.env.KUBECONFIG_FILE) {
opts.env.KUBECONFIG = "./kubeconfig.yml";
await writeFile(opts.env.KUBECONFIG, process.env.KUBECONFIG_FILE);
}
await writeFile("./values.yml", values);

if (deployment.task === "remove") {
exec.exec("helm", ["delete", release, "--purge"], opts);
// Actually execute the deployment here.
if (task === "remove") {
await exec.exec("helm", ["delete", release, "--purge"], opts);
} else {
exec.exec("helm", [
"upgrade", release, chart, "--install", "--wait", "--atomic",
"--namespace", namespace, "--values"
], opts);
await exec.exec("helm", args, opts);
}

await status("success");
} catch (error) {
core.error(error);
core.setFailed(error.message);
await status("failure");
}
}

Expand Down

0 comments on commit 2069c0b

Please sign in to comment.