Skip to content

Commit 8430ca0

Browse files
committed
remove legacy (non-buildx) $ docker build support
1 parent 13a8656 commit 8430ca0

File tree

1 file changed

+30
-78
lines changed

1 file changed

+30
-78
lines changed

cmd/bob/build.go

Lines changed: 30 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -185,92 +185,44 @@ func buildAndPushOneDockerImage(dockerImage bobfile.DockerImageSpec, buildCtx *B
185185

186186
printHeading(fmt.Sprintf("Building %s", tag))
187187

188-
// use buildx when platforms set. it's almost same as "$ docker build" but it almost transparently
189-
// supports cross-architecture builds via binftm_misc + QEMU userspace emulation
190-
useBuildx := len(dockerImage.Platforms) > 0
191-
192-
if useBuildx {
193-
// TODO: if in CI, install buildx automatically if needed?
194-
195-
args := []string{
196-
"buildx",
197-
"build",
198-
"--platform", strings.Join(dockerImage.Platforms, ","),
199-
"--file", dockerfilePath,
200-
"--tag=" + tag,
201-
}
202-
203-
// https://docs.docker.com/reference/cli/docker/buildx/build/#annotation
204-
// annotate both the image index and the image manifest.
205-
// if we don't specify type of annotation, only the image manifest is annotated.
206-
// for example GitHub packages UI only shows annotations from OCI image index.
207-
args = append(args, annotationsAs("--annotation=index,manifest:")...)
208-
209-
// for backwards compatibility (some consumers use this), publish the annotations also as labels
210-
args = append(args, annotationsAs("--label=")...)
211-
212-
if shouldTagLatest {
213-
args = append(args, "--tag="+tagLatest)
214-
}
215-
216-
args = append(args, buildContextDir)
188+
// TODO: if in CI, install buildx automatically if needed?
217189

218-
if buildCtx.PublishArtefacts {
219-
// the build command has integrated push support. we'd actually prefer to separate
220-
// these stages, but multi-arch manifests aren't supported storing locally so we've
221-
// to push immediately
222-
args = append(args, "--push")
223-
}
224-
225-
if err := passthroughStdoutAndStderr(exec.Command("docker", args...)).Run(); err != nil {
226-
return withErr(err)
227-
}
228-
} else {
229-
dockerBuildArgs := []string{"docker",
230-
"build",
231-
"--file", dockerfilePath,
232-
"--tag", tag}
233-
// `$ docker build ...` doesn't have annotation support. we have to use labels.
234-
dockerBuildArgs = append(dockerBuildArgs, annotationsAs("--label=")...)
235-
dockerBuildArgs = append(dockerBuildArgs, buildContextDir)
236-
237-
//nolint:gosec // ok
238-
buildCmd := passthroughStdoutAndStderr(exec.Command(dockerBuildArgs[0], dockerBuildArgs[1:]...))
239-
240-
if err := buildCmd.Run(); err != nil {
241-
return withErr(err)
242-
}
190+
args := []string{
191+
"docker",
192+
"buildx",
193+
"build",
194+
"--file", dockerfilePath,
195+
"--tag=" + tag,
196+
}
243197

244-
if buildCtx.PublishArtefacts {
245-
pushTag := func(tag string) error {
246-
printHeading(fmt.Sprintf("Pushing %s", tag))
198+
if len(dockerImage.Platforms) > 0 {
199+
args = append(args, "--platform="+strings.Join(dockerImage.Platforms, ","))
200+
}
247201

248-
pushCmd := passthroughStdoutAndStderr(exec.Command(
249-
"docker",
250-
"push",
251-
tag))
202+
// https://docs.docker.com/reference/cli/docker/buildx/build/#annotation
203+
// annotate both the image index and the image manifest.
204+
// if we don't specify type of annotation, only the image manifest is annotated.
205+
// for example GitHub packages UI only shows annotations from OCI image index.
206+
args = append(args, annotationsAs("--annotation=index,manifest:")...)
252207

253-
if err := pushCmd.Run(); err != nil {
254-
return err
255-
}
208+
// for backwards compatibility (some consumers use this), publish the annotations also as labels
209+
args = append(args, annotationsAs("--label=")...)
256210

257-
return nil
258-
}
211+
if shouldTagLatest {
212+
args = append(args, "--tag="+tagLatest)
213+
}
259214

260-
if err := pushTag(tag); err != nil {
261-
return withErr(err)
262-
}
215+
args = append(args, buildContextDir)
263216

264-
if shouldTagLatest {
265-
if err := exec.Command("docker", "tag", tag, tagLatest).Run(); err != nil {
266-
return withErr(fmt.Errorf("tagging failed %s -> %s failed: %v", tag, tagLatest, err))
267-
}
217+
if buildCtx.PublishArtefacts {
218+
// the build command has integrated push support. we'd actually prefer to separate
219+
// these stages, but multi-arch manifests aren't supported storing locally so we've
220+
// to push immediately
221+
args = append(args, "--push")
222+
}
268223

269-
if err := pushTag(tagLatest); err != nil {
270-
return withErr(err)
271-
}
272-
}
273-
}
224+
if err := passthroughStdoutAndStderr(exec.Command(args[0], args[1:]...)).Run(); err != nil {
225+
return withErr(err)
274226
}
275227

276228
return &imageBuildOutput{

0 commit comments

Comments
 (0)