-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
148 lines (132 loc) · 4.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const core = require("@actions/core");
const exec = require("@actions/exec");
const github = require("@actions/github");
const fs = require("fs");
const io = require("@actions/io");
const path = require("path");
const util = require("util");
const Mustache = require("mustache");
const writeFile = util.promisify(fs.writeFile);
const readFile = util.promisify(fs.readFile);
/**
* 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,
target_url: url,
headers: {
accept: "application/vnd.github.ant-man-preview+json"
}
});
} catch (error) {
core.warning(`Failed to set deployment status: ${error.message}`);
}
}
/**
* Get list parses an input with optional JSON syntax.
*
* @param {*} input
* @returns {Array<string>}
*/
function getList(files) {
let fileList;
if (typeof files === "string") {
try {
fileList = JSON.parse(files);
} catch (err) {
// Assume it's a single string.
fileList = [files];
}
} else {
fileList = files;
}
if (!Array.isArray(fileList)) {
return [];
}
return fileList.filter(f => !!f);
}
/**
* Render files renders data into the list of provided files.
* @param {Array<string>} files
* @param {any} data
*/
function renderFiles(files, data) {
core.debug(
`rendering files ${JSON.stringify(files)} 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);
}
/**
* Copies files over to dest
* @param {Array<string>} files
* @param {string} dst
*/
function copyFiles(files, dst) {
core.debug(`copying files ${JSON.stringify(files)} to ${dst}`);
const promises = files.map(src => io.cp(src, dst));
return Promise.all(promises);
}
async function run() {
try {
const context = github.context;
const remote = core.getInput("remote", { required: true });
const branch = core.getInput("branch", { required: true });
const target = core.getInput("target", { required: true });
const dryRun = core.getInput("dry-run") || false;
const email = core.getInput("email") || "bot@deliverybot.dev";
const name = core.getInput("name") || "bot[gitops]";
const manifests = getList(core.getInput("manifests", { required: true }));
core.debug(`param: remote = "${remote}"`);
core.debug(`param: branch = "${branch}"`);
core.debug(`param: manifests = ${JSON.stringify(manifests)}`);
await status("pending");
await exec.exec("git", ["config", "--global", "user.email", email]);
await exec.exec("git", ["config", "--global", "user.name", name]);
await exec.exec("git", [
"clone",
"--depth",
"1",
"--single-branch",
"--branch",
branch,
remote,
"target" // TODO: Make this a temp file.
]);
await renderFiles(manifests, context.payload);
await copyFiles(manifests, path.join("target", target));
await exec.exec("git", ["add", "."], { cwd: "./target" });
await exec.exec("git", ["commit", "-m", `Deploy`], { cwd: "./target" });
if (!dryRun) {
await exec.exec("git", ["push"], { cwd: "./target" });
}
await status("success");
} catch (error) {
core.error(error);
core.setFailed(error.message);
await status("failure");
}
}
run();