-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
132 lines (119 loc) · 4.53 KB
/
script.js
File metadata and controls
132 lines (119 loc) · 4.53 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
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
const http = require("http");
const { exec } = require("child_process");
const server = http.createServer((req, res) => {
if (req.method === "POST" && req.url === "/sahriar71/github-webhook-test") {
let body = "";
req.on("data", (chunk) => {
body += chunk.toString();
});
req.on("end", () => {
const payloadInJson = JSON.parse(body);
if (
payloadInJson?.action === "completed" &&
payloadInJson?.workflow_job?.name === "push_to_Docker_Hub"
) {
const imageName = "sahriar71/learning-forntend:latest";
exec(
`docker ps -q --filter ancestor=${imageName} | xargs -r docker stop \
&& docker ps -q --filter ancestor=${imageName} | xargs -r docker rm \
&& docker ps -a -q --filter ancestor=${imageName} | xargs -r docker rm`,
(error, stdout, stderr) => {
if (error) {
console.error(
`Error stopping/removing container: ${error.message}`
);
return;
}
console.log(`Stop and remove complete for ${imageName}`);
exec(
`cd /etc/project/Learning-Frontend ; git pull origin main; docker build -t ${imageName} . ;`,
(error, stdout, stderr) => {
if (error) {
console.error(`Error in build: ${error.message}`);
return;
}
console.log(`build complete ${imageName}`);
exec(
`docker run -d -p 8102:8101 ${imageName};`,
(error, stdout, stderr) => {
if (error) {
console.error(`Error in run: ${error.message}`);
exec(
`sudo kill -9 $(sudo lsof -t -i :8102); docker run -d -p 8102:8101 ${imageName};`,
(error, stdout, stderr) => {
if (error) {
console.error(`Error in run: ${error.message}`);
return;
}
console.log(`run complete ${imageName}`);
res.end("Received POST request successfully");
}
);
}
console.log(`run complete ${imageName}`);
res.end("Received POST request successfully");
}
);
}
);
}
);
}
});
} else if (req.url === "/sahriar71/learning-backend-webhook") {
let body = "";
req.on("data", (chunk) => {
body += chunk.toString();
});
req.on("end", () => {
const payloadInJson = JSON.parse(body);
if (payloadInJson?.push_data?.tag === "latest") {
const imageName = "sahriar71/learning-backend:latest";
console.log(`Webhook Req Found For ${imageName}`);
// Stop and remove existing container (if any)
console.log(
`Stop and remove existing container (if any) for ${imageName}`
);
exec(
`docker ps -q --filter ancestor=${imageName} | xargs -r docker stop \
&& docker ps -q --filter ancestor=${imageName} | xargs -r docker rm \
&& docker ps -a -q --filter ancestor=${imageName} | xargs -r docker rm`,
(error, stdout, stderr) => {
if (error) {
console.error(
`Error stopping/removing container: ${error.message}`
);
return;
}
console.log(`Stop and remove complete for ${imageName}`);
// Run a new container
console.log(`Run a new container for ${imageName}`);
exec(
`docker run -d -p 4001:3001 --env-file /etc/project/learning-backend/.env ${imageName}`,
(error, stdout, stderr) => {
if (error) {
console.error(
`Error running new container: ${error.message}`
);
return;
}
console.log(`Run complete for ${imageName}`);
console.log(
`Webhook received and deployment completed successfully for ${imageName}.`
);
res.end(`Compete for ${imageName}`);
}
);
}
);
}
});
} else {
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("Not Found");
}
});
const PORT = 2999;
server.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});