-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
93 lines (84 loc) · 2.29 KB
/
index.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
81
82
83
84
85
86
87
88
89
90
91
92
93
import { z, preprocess } from "zod";
import core from "@actions/core";
import github from "@actions/github";
const Colors = {
GREEN: "\x1b[92m",
BLUE: "\x1b[94m",
ORANGE: "\x1b[38;5;208m",
RED: "\x1b[91m",
GREY: "\x1b[90m",
ENDC: "\x1b[0m",
} as const;
const colors = {
green: (input: any) => `${Colors.GREEN}${input}${Colors.ENDC}`,
blue: (input: any) => `${Colors.BLUE}${input}${Colors.ENDC}`,
orange: (input: any) => `${Colors.ORANGE}${input}${Colors.ENDC}`,
red: (input: any) => `${Colors.RED}${input}${Colors.ENDC}`,
grey: (input: any) => `${Colors.GREY}${input}${Colors.ENDC}`,
} as const;
const inputSchema = z.object({
serviceImage: z.string().min(1, "The image of your service is required"),
webhookUrl: z
.string()
.url("Invalid URL for the webhook URL"),
commitMessage: z.string().optional(),
extraHeaders: preprocess((arg: any) => {
try {
return JSON.parse(arg);
} catch {
return {};
}
}, z.record(z.string(), z.string()).optional().default({})),
});
try {
const {
serviceImage,
webhookUrl,
commitMessage = `auto-deploy from commit ${github.context.sha}`,
extraHeaders,
} = inputSchema.parse({
serviceImage: process.env.SERVICE_IMAGE,
commitMessage: process.env.COMMIT_MESSAGE,
webhookUrl: process.env.DEPLOY_WEBHOOK_URL,
extraHeaders: process.env.EXTRA_HEADERS,
});
console.log(
`Deploying the service...`,
);
const deploymentResponse = await fetch(
webhookUrl,
{
method: "PUT",
headers: {
"content-type": "application/json",
...extraHeaders,
},
body: JSON.stringify({
commit_message: commitMessage,
new_image: serviceImage
}),
},
);
if (deploymentResponse.status === 200) {
console.log(`Deployment queued succesfully ✅`);
console.log(
`inspect in your dashboard 🙂`,
);
} else {
console.error(colors.red("❌ Failed to queue deployment ❌"));
console.error(
`Received status code from zaneops API : ${colors.red(
deploymentResponse.status,
)}`,
);
const response =
deploymentResponse.headers.get("content-type") === "application/json"
? await deploymentResponse.json()
: await deploymentResponse.text();
console.error("Received response from zaneops API : ");
console.dir(response);
core.setFailed("Failure");
}
} catch (error) {
core.setFailed((error as any).message);
}