Attach CI runner to edge E2E network

This commit is contained in:
Jeppe Bundgaard
2026-04-24 20:47:18 +02:00
parent 0f37bdc288
commit ab8923bb77
+70
View File
@@ -118,6 +118,73 @@ async function readDefaultGatewayHost() {
}
}
function composeNetworkName(composeProject) {
return `${composeProject}_default`;
}
async function readCurrentContainerRef() {
if (process.platform === "win32") {
return null;
}
const candidates = [];
const envHostname = String(process.env.HOSTNAME || "").trim();
if (envHostname !== "") {
candidates.push(envHostname);
}
try {
const hostname = (await fs.readFile("/etc/hostname", "utf8")).trim();
if (hostname !== "") {
candidates.push(hostname);
}
} catch {
// Not running in a container with /etc/hostname available.
}
try {
const cgroup = await fs.readFile("/proc/self/cgroup", "utf8");
const matches = cgroup.match(/[0-9a-f]{64}/gi) || [];
candidates.push(...matches);
} catch {
// cgroup metadata is optional in local development.
}
return candidates.find((candidate) => /^[a-zA-Z0-9][a-zA-Z0-9_.-]{1,127}$/.test(candidate)) || null;
}
async function connectCurrentContainerToComposeNetwork(rootDir, composeProject) {
const containerRef = await readCurrentContainerRef();
if (!containerRef) {
return false;
}
const inspection = await runCommand("docker", ["inspect", containerRef], {
cwd: rootDir,
allowFailure: true,
});
if (inspection.code !== 0) {
return false;
}
const networkName = composeNetworkName(composeProject);
const connection = await runCommand("docker", ["network", "connect", networkName, containerRef], {
cwd: rootDir,
allowFailure: true,
});
const stderr = String(connection.stderr || "");
if (connection.code === 0) {
process.stdout.write(`Attached runner container ${containerRef} to ${networkName}.\n`);
return true;
}
if (/already exists|already connected/i.test(stderr)) {
return true;
}
return false;
}
async function readComposeServiceHost(rootDir, composeProject, serviceName) {
const ps = await runCommand("docker", composeArgs(composeProject, ["ps", "-q", serviceName]), {
cwd: rootDir,
@@ -156,6 +223,9 @@ async function candidateApiBaseUrls(baseUrl, rootDir, composeProject) {
if (["localhost", "127.0.0.1", "::1"].includes(url.hostname)) {
if (rootDir && composeProject) {
await connectCurrentContainerToComposeNetwork(rootDir, composeProject);
candidates.push(baseUrlWithHost(normalized, "traefik", ""));
const traefikHost = await readComposeServiceHost(rootDir, composeProject, "traefik");
if (traefikHost) {
candidates.push(baseUrlWithHost(normalized, traefikHost, ""));