feat: add minimal frontend and backend CI demo

This commit is contained in:
2026-10-29 08:32:36 +00:00
parent 8b4495cb9f
commit 20182b4dae
6 changed files with 143 additions and 0 deletions

View File

@@ -1,2 +1,18 @@
# test-cicd-1 # test-cicd-1
Repo demo CI có hai Dockerfile để tạo hai service riêng:
| Service | Build context | Dockerfile | Port |
| --- | --- | --- | --- |
| `fe` | `fe` | `Dockerfile` | `8080` |
| `be` | `be` | `Dockerfile` | `8080` |
Trong Console, kết nối repo này, scan branch `main`, chọn cả hai Dockerfile, rồi đặt tên service lần lượt là `fe` và `be`. Cả hai dùng port container `8080`.
FE có trang đơn giản và gọi `GET /api/hello` qua server FE. Để thử kết nối nội bộ, đặt biến môi trường cho service `fe`:
```text
BE_URL=http://be:8080/api/hello
```
Tên host `be` cần khớp tên service và môi trường phải cho phép FE kết nối tới BE. Mỗi service có endpoint kiểm tra sức khỏe tại `/health`.

9
be/Dockerfile Normal file
View File

@@ -0,0 +1,9 @@
FROM node:22-alpine
WORKDIR /app
COPY server.js ./
ENV PORT=8080
EXPOSE 8080
USER node
CMD ["node", "server.js"]

26
be/server.js Normal file
View File

@@ -0,0 +1,26 @@
const { createServer } = require("node:http");
const port = Number(process.env.PORT || 8080);
function sendJson(response, status, body) {
response.writeHead(status, { "content-type": "application/json; charset=utf-8" });
response.end(JSON.stringify(body));
}
const server = createServer((request, response) => {
const pathname = new URL(request.url || "/", "http://localhost").pathname;
if (request.method === "GET" && (pathname === "/" || pathname === "/health")) {
return sendJson(response, 200, { status: "ok", service: "be" });
}
if (request.method === "GET" && pathname === "/api/hello") {
return sendJson(response, 200, { service: "be", message: "Hello from the backend" });
}
return sendJson(response, 404, { error: "Not found" });
});
server.listen(port, "0.0.0.0", () => {
console.log(`BE listening on port ${port}`);
});

9
fe/Dockerfile Normal file
View File

@@ -0,0 +1,9 @@
FROM node:22-alpine
WORKDIR /app
COPY server.js index.html ./
ENV PORT=8080
EXPOSE 8080
USER node
CMD ["node", "server.js"]

35
fe/index.html Normal file
View File

@@ -0,0 +1,35 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>FE / BE CI demo</title>
<style>
body { margin: 0; padding: 3rem 1.25rem; color: #172033; background: #f4f7fb; font: 16px/1.5 system-ui, sans-serif; }
main { max-width: 38rem; margin: auto; padding: 2rem; border: 1px solid #dbe3ef; border-radius: 1rem; background: white; box-shadow: 0 12px 32px #17203312; }
button { padding: .65rem 1rem; border: 0; border-radius: .5rem; color: white; background: #2458d3; font: inherit; cursor: pointer; }
pre { min-height: 3rem; padding: 1rem; overflow-wrap: anywhere; border-radius: .5rem; background: #f4f7fb; }
</style>
</head>
<body>
<main>
<h1>Frontend is running</h1>
<p>This page is served by the <code>fe</code> service.</p>
<button id="check-backend" type="button">Call backend</button>
<pre id="result" aria-live="polite">Click the button to call the backend.</pre>
</main>
<script>
const button = document.querySelector("#check-backend");
const result = document.querySelector("#result");
button.addEventListener("click", async () => {
result.textContent = "Calling backend…";
try {
const response = await fetch("/api/hello");
result.textContent = JSON.stringify(await response.json(), null, 2);
} catch {
result.textContent = "Could not reach the backend.";
}
});
</script>
</body>
</html>

48
fe/server.js Normal file
View File

@@ -0,0 +1,48 @@
const { createServer } = require("node:http");
const { readFile } = require("node:fs/promises");
const { join } = require("node:path");
const port = Number(process.env.PORT || 8080);
const backendUrl = process.env.BE_URL || "http://be:8080/api/hello";
function sendJson(response, status, body) {
response.writeHead(status, { "content-type": "application/json; charset=utf-8" });
response.end(JSON.stringify(body));
}
const server = createServer(async (request, response) => {
const pathname = new URL(request.url || "/", "http://localhost").pathname;
if (request.method === "GET" && pathname === "/health") {
return sendJson(response, 200, { status: "ok", service: "fe" });
}
if (request.method === "GET" && pathname === "/api/hello") {
try {
const backendResponse = await fetch(backendUrl, { signal: AbortSignal.timeout(3000) });
const body = await backendResponse.text();
response.writeHead(backendResponse.status, {
"content-type": backendResponse.headers.get("content-type") || "application/json; charset=utf-8",
});
return response.end(body);
} catch {
return sendJson(response, 502, { error: "Backend is unavailable", backendUrl });
}
}
if (request.method === "GET" && pathname === "/") {
try {
const html = await readFile(join(__dirname, "index.html"));
response.writeHead(200, { "content-type": "text/html; charset=utf-8" });
return response.end(html);
} catch {
return sendJson(response, 500, { error: "Frontend page is unavailable" });
}
}
return sendJson(response, 404, { error: "Not found" });
});
server.listen(port, "0.0.0.0", () => {
console.log(`FE listening on port ${port}`);
});