-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
deploy.ts
80 lines (70 loc) · 1.89 KB
/
deploy.ts
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
import { Args } from "../core/types.ts";
function deployHelp() {
const message = `USAGE
fastro deploy [OPTIONS]
OPTIONS
--name Application name
EXAMPLE
fastro deploy --name hello
`;
console.log(message);
}
async function getProjectId() {
try {
const p = Deno.run({
cmd: ["gcloud", "config", "list", "--format", "value(core.project)"],
stdout: "piped",
stderr: "piped",
});
const { code } = await p.status();
let str;
if (code === 0) {
const rawOutput = await p.output();
str = new TextDecoder().decode(rawOutput);
} else {
const rawError = await p.stderrOutput();
str = new TextDecoder().decode(rawError);
}
str = str.trim();
return str;
} catch (error) {
console.log("err", error);
}
}
async function cloudRunDeployHandler(projectId: string, appName: string) {
const image = `gcr.io/${projectId}/${appName}`;
const p = Deno.run({
cmd: [
"gcloud",
"run",
"deploy",
appName,
"--image",
image,
"--platform",
"managed",
"--allow-unauthenticated",
],
});
const { code } = await p.status();
if (code === 0) console.log({ message: "Deploy finish" });
else console.error({ message: "Deploy error" });
}
async function buildHandler(projectId: string, appName: string) {
const tag = `gcr.io/${projectId}/${appName}`;
const p = Deno.run({
cmd: ["gcloud", "builds", "submit", "--tag", tag],
});
const { code } = await p.status();
if (code === 0) console.log({ message: "Build Finish" });
else console.error({ message: "Build error" });
}
export async function handleDeploy(args: Args) {
if (args.help) return deployHelp();
const projectId = await getProjectId();
if (projectId) {
const appName = args.name ? args.name : "webapp";
await buildHandler(projectId, appName);
await cloudRunDeployHandler(projectId, appName);
}
}