Skip to content

Commit ed52e3c

Browse files
authored
feat: prefix cached image with ENVBUILDER_CACHED_IMAGE in log output (#251)
1 parent 8c5c50c commit ed52e3c

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

README.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,30 @@ docker run -it --rm \
226226

227227
Each layer is stored in the registry as a separate image. The image tag is the hash of the layer's contents. The image digest is the hash of the image tag. The image digest is used to pull the layer from the registry.
228228

229-
The performance improvement of builds depends on the complexity of your Dockerfile. For [`coder/coder`](https://github.com/coder/coder/blob/main/.devcontainer/Dockerfile), uncached builds take 36m while cached builds take 40s (~98% improvement).
229+
The performance improvement of builds depends on the complexity of your
230+
Dockerfile. For
231+
[`coder/coder`](https://github.com/coder/coder/blob/main/.devcontainer/Dockerfile),
232+
uncached builds take 36m while cached builds take 40s (~98% improvement).
233+
234+
## Pushing the built image
235+
236+
Set `ENVBUILDER_PUSH_IMAGE=1` to push the entire image to the cache repo
237+
in addition to individual layers. `ENVBUILDER_CACHE_REPO` **must** be set in
238+
order for this to work.
239+
240+
> **Note:** this option forces Envbuilder to perform a "reproducible" build.
241+
> This will force timestamps for all newly added files to be set to the start of the UNIX epoch.
242+
243+
## Probe Layer Cache
244+
245+
To check for the presence of a pre-built image, set
246+
`ENVBUILDER_GET_CACHED_IMAGE=1`. Instead of building the image, this will
247+
perform a "dry-run" build of the image, consulting `ENVBUILDER_CACHE_REPO` for
248+
each layer.
249+
250+
If any layer is found not to be present in the cache repo, envbuilder
251+
will exit with an error. Otherwise, the image will be emitted in the log output prefixed with the string
252+
`ENVBUILDER_CACHED_IMAGE=...`.
230253

231254
## Image Caching
232255

envbuilder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ ENTRYPOINT [%q]`, exePath, exePath, exePath)
566566
return nil, xerrors.Errorf("get cached image digest: %w", err)
567567
}
568568
endStage("🏗️ Found cached image!")
569-
_, _ = fmt.Fprintf(os.Stdout, "%s@%s\n", options.CacheRepo, digest.String())
569+
_, _ = fmt.Fprintf(os.Stdout, "ENVBUILDER_CACHED_IMAGE=%s@%s\n", options.CacheRepo, digest.String())
570570
os.Exit(0)
571571
}
572572

integration/integration_test.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -1172,19 +1172,30 @@ func TestPushImage(t *testing.T) {
11721172
}
11731173

11741174
// Then: re-running envbuilder with GET_CACHED_IMAGE should succeed
1175-
_, err = runEnvbuilder(t, options{env: []string{
1175+
ctrID, err := runEnvbuilder(t, options{env: []string{
11761176
envbuilderEnv("GIT_URL", srv.URL),
11771177
envbuilderEnv("CACHE_REPO", testRepo),
11781178
envbuilderEnv("GET_CACHED_IMAGE", "1"),
11791179
}})
11801180
require.NoError(t, err)
11811181

1182-
// When: we pull the image we just built
1182+
// Then: the cached image ref should be emitted in the container logs
11831183
ctx, cancel := context.WithCancel(context.Background())
11841184
t.Cleanup(cancel)
11851185
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
11861186
require.NoError(t, err)
11871187
defer cli.Close()
1188+
logs, err := cli.ContainerLogs(ctx, ctrID, container.LogsOptions{
1189+
ShowStdout: true,
1190+
ShowStderr: true,
1191+
})
1192+
require.NoError(t, err)
1193+
defer logs.Close()
1194+
logBytes, err := io.ReadAll(logs)
1195+
require.NoError(t, err)
1196+
require.Regexp(t, `ENVBUILDER_CACHED_IMAGE=(\S+)`, string(logBytes))
1197+
1198+
// When: we pull the image we just built
11881199
rc, err := cli.ImagePull(ctx, ref.String(), image.PullOptions{})
11891200
require.NoError(t, err)
11901201
t.Cleanup(func() { _ = rc.Close() })

0 commit comments

Comments
 (0)