Skip to content

Commit ab5c1da

Browse files
pbardeacursoragent
andauthored
fix: keep docker mirror on local builder fallback (#105)
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 722e97d commit ab5c1da

5 files changed

Lines changed: 79 additions & 24 deletions

File tree

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
pruneBuildkitCache,
2626
logBuildCacheContents,
2727
logDatabaseHashes,
28+
writeDockerContainerBuildkitdTomlFile,
2829
} from "./setup_builder";
2930
import {
3031
installBuildKit,
@@ -739,9 +740,11 @@ void actionsToolkit.run(
739740
`Found configured builder: ${builder.name} (driver: ${builder.driver})`,
740741
);
741742
} else {
742-
// Create a local builder
743-
const createLocalBuilderCmd =
744-
"docker buildx create --name local --driver docker-container --use";
743+
// Create a local builder with the same Docker Hub mirror config as
744+
// the Blacksmith builder so sticky disk failures do not bypass it.
745+
const localBuilderConfigPath =
746+
await writeDockerContainerBuildkitdTomlFile();
747+
const createLocalBuilderCmd = `docker buildx create --name local --driver docker-container --config ${localBuilderConfigPath} --use`;
745748
try {
746749
await Exec.exec(createLocalBuilderCmd);
747750
core.info("Created and set a local builder for use");

src/setup-builder.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, it, expect, vi, beforeEach } from "vitest";
22
import * as core from "@actions/core";
3+
import * as fs from "fs";
34
import * as setupBuilder from "./setup_builder";
45
// import * as reporter from "./reporter";
56

@@ -84,6 +85,26 @@ describe("setup_builder", () => {
8485

8586
// Tailscale tests removed - not needed for setup-docker-builder
8687

88+
describe("writeDockerContainerBuildkitdTomlFile", () => {
89+
it("writes a docker-container BuildKit config with the Docker mirror", async () => {
90+
const writeFile = vi.mocked(fs.promises.writeFile);
91+
92+
await setupBuilder.writeDockerContainerBuildkitdTomlFile(
93+
"local-buildkitd.toml",
94+
);
95+
96+
expect(writeFile.mock.calls[0][0]).toBe("local-buildkitd.toml");
97+
const config = writeFile.mock.calls[0][1] as string;
98+
expect(config).toContain('mirrors = [ "http://192.168.127.1:5000" ]');
99+
expect(config).toContain('[registry."docker.io"]');
100+
expect(config).toContain('[registry."192.168.127.1:5000"]');
101+
expect(config).not.toContain("[grpc]");
102+
expect(core.info).toHaveBeenCalledWith(
103+
"Wrote Docker container BuildKit config to local-buildkitd.toml",
104+
);
105+
});
106+
});
107+
87108
describe("logBuildCacheContents", () => {
88109
it("should log build cache contents from buildctl du", async () => {
89110
const exec = (await import("child_process")).exec as unknown as {

src/setup_builder.ts

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,35 @@ async function getRoutableHostDns(): Promise<string[]> {
133133
return publicDnsFallback;
134134
}
135135

136+
function getDockerMirrorRegistryConfig(): TOML.JsonMap {
137+
return {
138+
"docker.io": {
139+
mirrors: ["http://192.168.127.1:5000"],
140+
http: true,
141+
insecure: true,
142+
},
143+
"192.168.127.1:5000": {
144+
http: true,
145+
insecure: true,
146+
},
147+
};
148+
}
149+
150+
async function writeTomlConfig(
151+
filePath: string,
152+
jsonConfig: TOML.JsonMap,
153+
): Promise<void> {
154+
const tomlString = TOML.stringify(jsonConfig);
155+
156+
try {
157+
await fs.promises.writeFile(filePath, tomlString);
158+
core.debug(`TOML configuration is ${tomlString}`);
159+
} catch (err) {
160+
core.warning(`error writing TOML configuration: ${(err as Error).message}`);
161+
throw err;
162+
}
163+
}
164+
136165
async function writeBuildkitdTomlFile(
137166
parallelism: number,
138167
addr: string,
@@ -151,17 +180,7 @@ async function writeBuildkitdTomlFile(
151180
dns: {
152181
nameservers: dnsNameservers,
153182
},
154-
registry: {
155-
"docker.io": {
156-
mirrors: ["http://192.168.127.1:5000"],
157-
http: true,
158-
insecure: true,
159-
},
160-
"192.168.127.1:5000": {
161-
http: true,
162-
insecure: true,
163-
},
164-
},
183+
registry: getDockerMirrorRegistryConfig(),
165184
worker: {
166185
oci: {
167186
enabled: true,
@@ -177,15 +196,27 @@ async function writeBuildkitdTomlFile(
177196
},
178197
};
179198

180-
const tomlString = TOML.stringify(jsonConfig);
199+
await writeTomlConfig("buildkitd.toml", jsonConfig);
200+
}
181201

182-
try {
183-
await fs.promises.writeFile("buildkitd.toml", tomlString);
184-
core.debug(`TOML configuration is ${tomlString}`);
185-
} catch (err) {
186-
core.warning(`error writing TOML configuration: ${(err as Error).message}`);
187-
throw err;
188-
}
202+
export async function writeDockerContainerBuildkitdTomlFile(
203+
filePath = "docker-container-buildkitd.toml",
204+
): Promise<string> {
205+
// Keep this config safe for buildx's docker-container driver: it should not
206+
// set grpc.address, which buildx owns for the containerized BuildKit daemon.
207+
await configureSystemdResolvedForBuildkit();
208+
const dnsNameservers = await getRoutableHostDns();
209+
const jsonConfig: TOML.JsonMap = {
210+
dns: {
211+
nameservers: dnsNameservers,
212+
},
213+
registry: getDockerMirrorRegistryConfig(),
214+
};
215+
216+
await writeTomlConfig(filePath, jsonConfig);
217+
core.info(`Wrote Docker container BuildKit config to ${filePath}`);
218+
219+
return filePath;
189220
}
190221

191222
export async function startBuildkitd(

0 commit comments

Comments
 (0)