Skip to content

Commit 7d47527

Browse files
committed
history: make sure build record is finalized before exporting
Signed-off-by: CrazyMax <[email protected]>
1 parent 9998ef7 commit 7d47527

File tree

4 files changed

+78
-49
lines changed

4 files changed

+78
-49
lines changed

commands/history/export.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ import (
2020
)
2121

2222
type exportOptions struct {
23-
builder string
24-
refs []string
25-
output string
26-
all bool
23+
builder string
24+
refs []string
25+
output string
26+
all bool
27+
finalize bool
2728
}
2829

2930
func runExport(ctx context.Context, dockerCli command.Cli, opts exportOptions) error {
@@ -62,6 +63,25 @@ func runExport(ctx context.Context, dockerCli command.Cli, opts exportOptions) e
6263
return errors.Errorf("no record found for ref %q", ref)
6364
}
6465

66+
if opts.finalize {
67+
var finalize bool
68+
for _, rec := range recs {
69+
if rec.Trace == nil {
70+
finalize = true
71+
break
72+
}
73+
}
74+
if finalize {
75+
recs, err = queryRecords(ctx, ref, nodes, &queryOptions{
76+
CompletedOnly: true,
77+
Finalize: true,
78+
})
79+
if err != nil {
80+
return err
81+
}
82+
}
83+
}
84+
6585
if ref == "" {
6686
slices.SortFunc(recs, func(a, b historyRecord) int {
6787
return b.CreatedAt.AsTime().Compare(a.CreatedAt.AsTime())
@@ -154,7 +174,8 @@ func exportCmd(dockerCli command.Cli, rootOpts RootOptions) *cobra.Command {
154174

155175
flags := cmd.Flags()
156176
flags.StringVarP(&options.output, "output", "o", "", "Output file path")
157-
flags.BoolVar(&options.all, "all", false, "Export all records for the builder")
177+
flags.BoolVar(&options.all, "all", false, "Export all build records for the builder")
178+
flags.BoolVar(&options.finalize, "finalize", false, "Ensure build records are finalized before exporting")
158179

159180
return cmd
160181
}

commands/history/trace.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/docker/buildx/util/otelutil"
1818
"github.com/docker/buildx/util/otelutil/jaeger"
1919
"github.com/docker/cli/cli/command"
20-
controlapi "github.com/moby/buildkit/api/services/control"
2120
"github.com/opencontainers/go-digest"
2221
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
2322
"github.com/pkg/browser"
@@ -57,20 +56,9 @@ func loadTrace(ctx context.Context, ref string, nodes []builder.Node) (string, [
5756
// build is complete but no trace yet. try to finalize the trace
5857
time.Sleep(1 * time.Second) // give some extra time for last parts of trace to be written
5958

60-
c, err := rec.node.Driver.Client(ctx)
61-
if err != nil {
62-
return "", nil, err
63-
}
64-
_, err = c.ControlClient().UpdateBuildHistory(ctx, &controlapi.UpdateBuildHistoryRequest{
65-
Ref: rec.Ref,
66-
Finalize: true,
67-
})
68-
if err != nil {
69-
return "", nil, err
70-
}
71-
7259
recs, err := queryRecords(ctx, rec.Ref, []builder.Node{*rec.node}, &queryOptions{
7360
CompletedOnly: true,
61+
Finalize: true,
7462
})
7563
if err != nil {
7664
return "", nil, err

commands/history/utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ type historyRecord struct {
116116
type queryOptions struct {
117117
CompletedOnly bool
118118
Filters []string
119+
Finalize bool
119120
}
120121

121122
func queryRecords(ctx context.Context, ref string, nodes []builder.Node, opts *queryOptions) ([]historyRecord, error) {
@@ -163,6 +164,15 @@ func queryRecords(ctx context.Context, ref string, nodes []builder.Node, opts *q
163164
filters = []string{strings.TrimSuffix(sb.String(), "\n")}
164165
}
165166

167+
if opts.Finalize {
168+
if _, err := c.ControlClient().UpdateBuildHistory(ctx, &controlapi.UpdateBuildHistoryRequest{
169+
Ref: ref,
170+
Finalize: true,
171+
}); err != nil {
172+
return err
173+
}
174+
}
175+
166176
serv, err := c.ControlClient().ListenBuildHistory(ctx, &controlapi.BuildHistoryRequest{
167177
EarlyExit: true,
168178
Ref: ref,

docs/reference/buildx_history_export.md

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ Export a build into Docker Desktop bundle
55

66
### Options
77

8-
| Name | Type | Default | Description |
9-
|:---------------------------------------|:---------|:--------|:-----------------------------------------|
10-
| [`--all`](#all) | `bool` | | Export all records for the builder |
11-
| [`--builder`](#builder) | `string` | | Override the configured builder instance |
12-
| [`-D`](#debug), [`--debug`](#debug) | `bool` | | Enable debug logging |
13-
| [`-o`](#output), [`--output`](#output) | `string` | | Output file path |
8+
| Name | Type | Default | Description |
9+
|:---------------------------------------|:---------|:--------|:----------------------------------------------------|
10+
| [`--all`](#all) | `bool` | | Export all build records for the builder |
11+
| [`--builder`](#builder) | `string` | | Override the configured builder instance |
12+
| [`-D`](#debug), [`--debug`](#debug) | `bool` | | Enable debug logging |
13+
| [`--finalize`](#finalize) | `bool` | | Ensure build records are finalized before exporting |
14+
| [`-o`](#output), [`--output`](#output) | `string` | | Output file path |
1415

1516

1617
<!---MARKER_GEN_END-->
@@ -23,59 +24,68 @@ Desktop or shared across environments.
2324

2425
## Examples
2526

26-
### <a name="output"></a> Export a single build to a custom file (--output)
27+
### <a name="all"></a> Export all build records to a file (--all)
28+
29+
Use the `--all` flag and redirect the output:
2730

2831
```console
29-
docker buildx history export qu2gsuo8ejqrwdfii23xkkckt --output mybuild.dockerbuild
32+
docker buildx history export --all > all-builds.dockerbuild
3033
```
3134

32-
You can find build IDs by running:
35+
Or use the `--output` flag:
3336

3437
```console
35-
docker buildx history ls
38+
docker buildx history export --all -o all-builds.dockerbuild
3639
```
3740

38-
### <a name="o"></a> Export multiple builds to individual `.dockerbuild` files (-o)
39-
40-
To export two builds to separate files:
41+
### <a name="builder"></a> Use a specific builder instance (--builder)
4142

4243
```console
43-
# Using build IDs
44-
docker buildx history export qu2gsuo8ejqrwdfii23xkkckt qsiifiuf1ad9pa9qvppc0z1l3 -o multi.dockerbuild
45-
46-
# Or using relative offsets
47-
docker buildx history export ^1 ^2 -o multi.dockerbuild
44+
docker buildx history export --builder builder0 ^1 -o builder0-build.dockerbuild
4845
```
4946

50-
Or use shell redirection:
47+
### <a name="debug"></a> Enable debug logging (--debug)
5148

5249
```console
53-
docker buildx history export ^1 > mybuild.dockerbuild
54-
docker buildx history export ^2 > backend-build.dockerbuild
50+
docker buildx history export --debug qu2gsuo8ejqrwdfii23xkkckt -o debug-build.dockerbuild
5551
```
5652

57-
### <a name="all"></a> Export all build records to a file (--all)
53+
### <a name="finalize"></a> Ensure build records are finalized before exporting (--finalize)
5854

59-
Use the `--all` flag and redirect the output:
55+
Clients can report their own traces concurrently and not all traces may be
56+
finalized when a build is completed. Use the `--finalize` flag to ensure all
57+
traces are finalized before exporting. It should only be used if you are sure
58+
that the build is completed.
6059

6160
```console
62-
docker buildx history export --all > all-builds.dockerbuild
61+
docker buildx history export --finalize qu2gsuo8ejqrwdfii23xkkckt -o finalized-build.dockerbuild
6362
```
6463

65-
Or use the `--output` flag:
64+
### <a name="output"></a> Export a single build to a custom file (--output)
6665

6766
```console
68-
docker buildx history export --all -o all-builds.dockerbuild
67+
docker buildx history export qu2gsuo8ejqrwdfii23xkkckt --output mybuild.dockerbuild
6968
```
7069

71-
### <a name="builder"></a> Use a specific builder instance (--builder)
70+
You can find build IDs by running:
7271

7372
```console
74-
docker buildx history export --builder builder0 ^1 -o builder0-build.dockerbuild
73+
docker buildx history ls
7574
```
7675

77-
### <a name="debug"></a> Enable debug logging (--debug)
76+
To export two builds to separate files:
7877

7978
```console
80-
docker buildx history export --debug qu2gsuo8ejqrwdfii23xkkckt -o debug-build.dockerbuild
81-
```
79+
# Using build IDs
80+
docker buildx history export qu2gsuo8ejqrwdfii23xkkckt qsiifiuf1ad9pa9qvppc0z1l3 -o multi.dockerbuild
81+
82+
# Or using relative offsets
83+
docker buildx history export ^1 ^2 -o multi.dockerbuild
84+
```
85+
86+
Or use shell redirection:
87+
88+
```console
89+
docker buildx history export ^1 > mybuild.dockerbuild
90+
docker buildx history export ^2 > backend-build.dockerbuild
91+
```

0 commit comments

Comments
 (0)