Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit fd10e25

Browse files
committed
Create bundle from tag, or compute a digest
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent b817045 commit fd10e25

1 file changed

Lines changed: 53 additions & 24 deletions

File tree

internal/commands/build.go

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"encoding/json"
77
"fmt"
88
"github.com/docker/app/internal/packager"
9+
"github.com/docker/distribution/reference"
910
"github.com/moby/buildkit/session"
1011
"github.com/moby/buildkit/session/auth/authprovider"
12+
"github.com/opencontainers/go-digest"
1113
"github.com/sirupsen/logrus"
1214
"os"
1315
"path/filepath"
@@ -43,7 +45,11 @@ func buildCmd(dockerCli command.Cli) *cobra.Command {
4345
Example: `$ docker app build myapp.dockerapp`,
4446
Args: cli.RequiresRangeArgs(1, 1),
4547
RunE: func(cmd *cobra.Command, args []string) error {
46-
return runBuild(dockerCli, args[0], opts)
48+
tag, err := runBuild(dockerCli, args[0], opts)
49+
if err == nil {
50+
fmt.Printf("Successfully build %s\n", tag.String())
51+
}
52+
return err
4753
},
4854
}
4955

@@ -56,30 +62,30 @@ func buildCmd(dockerCli command.Cli) *cobra.Command {
5662
return cmd
5763
}
5864

59-
func runBuild(dockerCli command.Cli, application string, opt buildOptions) error {
65+
func runBuild(dockerCli command.Cli, application string, opt buildOptions) (reference.Named, error) {
6066
app, err := packager.Extract(application)
6167
if err != nil {
62-
return err
68+
return nil, err
6369
}
6470
defer app.Cleanup()
6571
appname := app.Name
6672

6773
bundle, err := makeBundleFromApp(dockerCli, app, nil)
6874
if err != nil {
69-
return err
75+
return nil, err
7076
}
7177

7278
ctx := appcontext.Context()
7379
compose, err := bake.ParseCompose(app.Composes()[0]) // Fixme can have > 1 composes ?
7480
if err != nil {
75-
return err
81+
return nil, err
7682
}
7783

7884
targets := map[string]bake.Target{}
7985
for _, n := range compose.ResolveGroup("default") {
8086
t, err := compose.ResolveTarget(n)
8187
if err != nil {
82-
return nil
88+
return nil, err
8389
}
8490
if t != nil {
8591
targets[n] = *t
@@ -92,44 +98,42 @@ func runBuild(dockerCli command.Cli, application string, opt buildOptions) error
9298
// FIXME docker app init should maybe udate them ?
9399
path, err := filepath.Abs(appname + "/../" + (*t.Context)[1:])
94100
if err != nil {
95-
return err
101+
return nil, err
96102
}
97103
t.Context = &path
98-
t.Tags = []string{fmt.Sprintf("%s:%s-%s", bundle.Name, bundle.Version, service)}
99104
targets[service] = t
100105
}
101106
}
102107

103108
if logrus.IsLevelEnabled(logrus.DebugLevel) {
104109
dt, err := json.MarshalIndent(map[string]map[string]bake.Target{"target": targets}, "", " ")
105110
if err != nil {
106-
return err
111+
return nil, err
107112
}
108113
logrus.Debug(string(dt))
109114
}
110115

111116
buildopts, err := bake.TargetsToBuildOpt(targets, opt.noCache, opt.pull)
112117
if err != nil {
113-
return err
118+
return nil, err
114119
}
115120

116121
buildContext := bytes.NewBuffer(nil)
117122
if err := packager.PackInvocationImageContext(dockerCli, app, buildContext); err != nil {
118-
return err
123+
return nil, err
119124
}
120125

121126
buildopts["invocation-image"] = build.Options{
122127
Inputs: build.Inputs{
123128
InStream: buildContext,
124129
ContextPath: "-",
125130
},
126-
Tags: []string{ fmt.Sprintf("%s:%s-%s", bundle.Name, bundle.Version, "-invoc") },
127131
Session: []session.Attachable{authprovider.NewDockerAuthProvider(os.Stderr)},
128132
}
129133

130134
d, err := driver.GetDriver(ctx, "buildx_buildkit_default", nil, dockerCli.Client(), nil, "", nil)
131135
if err != nil {
132-
return err
136+
return nil, err
133137
}
134138
driverInfo := []build.DriverInfo{
135139
{
@@ -144,7 +148,7 @@ func runBuild(dockerCli command.Cli, application string, opt buildOptions) error
144148
pw := progress.NewPrinter(ctx2, os.Stderr, opt.progress)
145149
resp, err := build.Build(ctx2, driverInfo, buildopts, dockerAPI(dockerCli), dockerCli.ConfigFile(), pw)
146150
if err != nil {
147-
return err
151+
return nil, err
148152
}
149153

150154
fmt.Println("Successfully built service images")
@@ -166,27 +170,52 @@ func runBuild(dockerCli command.Cli, application string, opt buildOptions) error
166170
if logrus.IsLevelEnabled(logrus.DebugLevel) {
167171
dt, err := json.MarshalIndent(resp, "", " ")
168172
if err != nil {
169-
return err
173+
return nil, err
170174
}
171175
logrus.Debug(string(dt))
172176
}
173177

174-
if opt.tag == "" {
175-
opt.tag = bundle.Name + ":" + bundle.Version
176-
}
177-
178-
ref, err := getNamedTagged(opt.tag)
178+
var ref reference.Named
179+
ref, err = getNamedTagged(opt.tag)
179180
if err != nil {
180-
return err
181+
return nil, err
182+
}
183+
if ref == nil {
184+
b := bytes.Buffer{}
185+
_, err := bundle.WriteTo(&b)
186+
if err != nil {
187+
return nil, err
188+
}
189+
digest := digest.SHA256.FromBytes(b.Bytes())
190+
ref = sha{digest}
181191
}
182-
183192
if err := persistInBundleStore(ref, bundle); err != nil {
184-
return err
193+
return nil, err
185194
}
186195

187-
return nil
196+
return ref, nil
188197
}
189198

199+
type sha struct {
200+
d digest.Digest
201+
}
202+
203+
func (s sha) Digest() digest.Digest {
204+
return s.d
205+
}
206+
207+
func (s sha) String() string {
208+
return s.d.String()
209+
}
210+
211+
func (s sha) Name() string {
212+
return s.d.String()
213+
}
214+
215+
var _ reference.Named = sha{""}
216+
var _ reference.Digested = sha{""}
217+
218+
190219
/// FIXME copy from vendor/github.com/docker/buildx/commands/util.go:318 could probably be made public
191220
func dockerAPI(dockerCli command.Cli) *api {
192221
return &api{dockerCli: dockerCli}

0 commit comments

Comments
 (0)