diff --git a/action.yml b/action.yml index 6ed6b24f..2542a49e 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/index.js b/index.js index 3a34349d..cc5d8223 100644 --- a/index.js +++ b/index.js @@ -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"); } }