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

Commit 2177388

Browse files
committed
produce bundle
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent 3194afc commit 2177388

2 files changed

Lines changed: 72 additions & 10 deletions

File tree

internal/commands/build.go

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package commands
22

33
import (
4+
"context"
5+
"encoding/json"
46
"fmt"
57
"os"
68
"path/filepath"
@@ -10,6 +12,7 @@ import (
1012

1113
"github.com/docker/buildx/util/progress"
1214

15+
cnab "github.com/deislabs/cnab-go/driver"
1316
"github.com/docker/app/internal"
1417
"github.com/docker/buildx/bake"
1518
"github.com/docker/buildx/build"
@@ -26,6 +29,7 @@ type buildOptions struct {
2629
noCache bool
2730
progress string
2831
pull bool
32+
tag string
2933
}
3034

3135
func buildCmd(dockerCli command.Cli) *cobra.Command {
@@ -44,40 +48,57 @@ func buildCmd(dockerCli command.Cli) *cobra.Command {
4448
flags.BoolVar(&opts.noCache, "no-cache", false, "Do not use cache when building the image")
4549
flags.StringVar(&opts.progress, "progress", "auto", "Set type of progress output (auto, plain, tty). Use plain to show container output")
4650
flags.BoolVar(&opts.pull, "pull", false, "Always attempt to pull a newer version of the image")
51+
cmd.Flags().StringVarP(&opts.tag, "tag", "t", "", "Name and optionally a tag in the 'name:tag' format")
4752

4853
return cmd
4954
}
5055

5156
func runBuild(dockerCli command.Cli, application string, opt buildOptions) error {
52-
f := application + "/" + internal.ComposeFileName
57+
appname := internal.DirNameFromAppName(application)
58+
f := "./" + appname + "/" + internal.ComposeFileName
5359
if _, err := os.Stat(f); err != nil {
5460
if os.IsNotExist(errors.Cause(err)) {
5561
return fmt.Errorf("no compose file at %s, did you selected the right docker app folder ?", f)
5662
}
5763
}
5864

65+
bundle, err := makeBundle(dockerCli, application, nil)
66+
if err != nil {
67+
return err
68+
}
69+
5970
ctx := appcontext.Context()
60-
cfg, err := bake.ParseFile(f)
71+
targets, err := bake.ReadTargets(ctx, []string{f}, []string{"default"}, nil)
6172
if err != nil {
6273
return err
6374
}
64-
for k, t := range cfg.Target {
75+
76+
for k, t := range targets {
6577
if strings.HasPrefix(*t.Context, ".") {
66-
path, err := filepath.Abs(application + "/" + (*t.Context)[1:])
78+
// Relative path in compose file under x.dockerapp refers to parent folder
79+
// FIXME docker app init should maybe udate them ?
80+
path, err := filepath.Abs(appname + "/../" + (*t.Context)[1:])
6781
if err != nil {
6882
return err
6983
}
7084
t.Context = &path
71-
cfg.Target[k] = t
85+
t.Tags = []string{fmt.Sprintf("%s:%s", bundle.Name, bundle.Version)}
86+
targets[k] = t
7287
}
7388
}
7489

75-
buildopts, err := bake.TargetsToBuildOpt(cfg.Target, opt.noCache, opt.pull)
90+
// -- debug
91+
dt, err := json.MarshalIndent(map[string]map[string]bake.Target{"target": targets}, "", " ")
7692
if err != nil {
7793
return err
7894
}
95+
fmt.Fprintln(dockerCli.Out(), string(dt))
96+
// -- debug
7997

80-
pw := progress.NewPrinter(ctx, os.Stderr, opt.progress)
98+
buildopts, err := bake.TargetsToBuildOpt(targets, opt.noCache, opt.pull)
99+
if err != nil {
100+
return err
101+
}
81102

82103
d, err := driver.GetDriver(ctx, "buildx_buildkit_default", nil, dockerCli.Client(), nil, "", nil)
83104
if err != nil {
@@ -90,8 +111,49 @@ func runBuild(dockerCli command.Cli, application string, opt buildOptions) error
90111
},
91112
}
92113

93-
_, err = build.Build(ctx, driverInfo, buildopts, dockerAPI(dockerCli), dockerCli.ConfigFile(), pw)
94-
return err
114+
ctx2, cancel := context.WithCancel(context.TODO())
115+
defer cancel()
116+
117+
// FIXME add invocation image as another build target
118+
119+
pw := progress.NewPrinter(ctx2, os.Stderr, opt.progress)
120+
resp, err := build.Build(ctx2, driverInfo, buildopts, dockerAPI(dockerCli), dockerCli.ConfigFile(), pw)
121+
if err != nil {
122+
return err
123+
}
124+
125+
fmt.Println("Successfully built service images")
126+
for k, r := range resp {
127+
digest := r.ExporterResponse["containerimage.digest"]
128+
image := bundle.Images[k]
129+
image.ImageType = cnab.ImageTypeDocker
130+
image.Digest = digest
131+
bundle.Images[k] = image
132+
fmt.Printf(" - %s : %s\n", k, image.Digest)
133+
}
134+
135+
// -- debug
136+
dt, err = json.MarshalIndent(resp, "", " ")
137+
if err != nil {
138+
return err
139+
}
140+
fmt.Fprintln(dockerCli.Out(), string(dt))
141+
// -- debug
142+
143+
if opt.tag == "" {
144+
opt.tag = bundle.Name + ":" + bundle.Version
145+
}
146+
147+
ref, err := getNamedTagged(opt.tag)
148+
if err != nil {
149+
return err
150+
}
151+
152+
if err := persistInBundleStore(ref, bundle); err != nil {
153+
return err
154+
}
155+
156+
return nil
95157
}
96158

97159
/// FIXME copy from vendor/github.com/docker/buildx/commands/util.go:318 could probably be made public

internal/commands/cnab.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func getAppNameKind(name string) (string, nameKind) {
215215
if name == "" {
216216
return name, nameKindEmpty
217217
}
218-
// name can be a bundle.json or bundle.cnab file, a single dockerapp file, or a dockerapp directory
218+
// name can be a bundle.json or bundle.cnab file, or a dockerapp directory
219219
st, err := os.Stat(name)
220220
if os.IsNotExist(err) {
221221
// try with .dockerapp extension

0 commit comments

Comments
 (0)