-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook.js
More file actions
73 lines (62 loc) · 2.02 KB
/
Copy pathwebhook.js
File metadata and controls
73 lines (62 loc) · 2.02 KB
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
import http from "http";
import { exec } from "child_process";
import crypto from "crypto";
const WEBHOOK_SECRET = "idktbh"; // Change this!
const PORT = 9191;
const server = http.createServer((req, res) => {
// Health check endpoint for ngrok
if (req.url === "/health") {
res.writeHead(200);
res.end("ok");
return;
}
if (req.url === "/webhook" && req.method === "POST") {
let body = "";
req.on("data", (chunk) => {
body += chunk.toString();
});
req.on("end", () => {
// Verify GitHub signature (optional but recommended)
const sig = req.headers["x-hub-signature-256"] || "";
const expectedSig =
"sha256=" +
crypto.createHmac("sha256", WEBHOOK_SECRET).update(body).digest("hex");
if (sig !== expectedSig) {
console.log("[webhook] Invalid signature, ignoring.");
res.writeHead(403);
res.end("Forbidden");
return;
}
const event = req.headers["x-github-event"];
console.log(`[webhook] Received event: ${event}`);
if (event === "push") {
const payload = JSON.parse(body);
const branch = payload.ref?.replace("refs/heads/", "");
console.log(`[webhook] Push to branch: ${branch}`);
// Only deploy pushes to main/master
if (branch === "main" || branch === "master") {
console.log("[webhook] Triggering deploy...");
exec(
"bash /var/home/vsht/Documents/DP-collab-project/deploy.sh",
(err, stdout, stderr) => {
if (err) {
console.error("[webhook] Deploy failed:", stderr);
} else {
console.log("[webhook] Deploy completed:", stdout);
}
},
);
}
}
res.writeHead(200);
res.end("ok");
});
} else {
res.writeHead(404);
res.end("Not found");
}
});
server.listen(PORT, () => {
console.log(`[webhook] Listening on port ${PORT}`);
console.log(`[webhook] Webhook endpoint: http://localhost:${PORT}/webhook`);
});