Skip to content

Commit

Permalink
build: allow setting buildkit outputs
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Mar 21, 2019
1 parent 774d78f commit 304e992
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
53 changes: 53 additions & 0 deletions cli/command/image/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bufio"
"bytes"
"context"
"encoding/csv"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -73,6 +74,7 @@ type buildOptions struct {
untrusted bool
secrets []string
ssh []string
outputs []string
}

// dockerfileFromStdin returns true when the user specified that the Dockerfile
Expand Down Expand Up @@ -176,6 +178,11 @@ func NewBuildCommand(dockerCli command.Cli) *cobra.Command {
flags.StringArrayVar(&options.ssh, "ssh", []string{}, "SSH agent socket or keys to expose to the build (only if BuildKit enabled) (format: default|<id>[=<socket>|<key>[,<key>]])")
flags.SetAnnotation("ssh", "version", []string{"1.39"})
flags.SetAnnotation("ssh", "buildkit", nil)

flags.StringArrayVarP(&options.outputs, "output", "o", []string{}, "Output destination (format: type=local,dest=path)")
flags.SetAnnotation("output", "version", []string{"1.40"})
flags.SetAnnotation("output", "buildkit", nil)

return cmd
}

Expand Down Expand Up @@ -643,3 +650,49 @@ func imageBuildOptions(dockerCli command.Cli, options buildOptions) types.ImageB
Platform: options.platform,
}
}

func parseOutputs(inp []string) ([]types.ImageBuildOutput, error) {
var outs []types.ImageBuildOutput
if len(inp) == 0 {
return nil, nil
}
for _, s := range inp {
csvReader := csv.NewReader(strings.NewReader(s))
fields, err := csvReader.Read()
if err != nil {
return nil, err
}
if len(fields) == 1 && fields[0] == s {
outs = append(outs, types.ImageBuildOutput{
Type: "local",
Attrs: map[string]string{
"dest": s,
},
})
continue
}

out := types.ImageBuildOutput{
Attrs: map[string]string{},
}
for _, field := range fields {
parts := strings.SplitN(field, "=", 2)
if len(parts) != 2 {
return nil, errors.Errorf("invalid value %s", field)
}
key := strings.ToLower(parts[0])
value := parts[1]
switch key {
case "type":
out.Type = value
default:
out.Attrs[key] = value
}
}
if out.Type == "" {
return nil, errors.Errorf("type is required for output")
}
outs = append(outs, out)
}
return outs, nil
}
17 changes: 17 additions & 0 deletions cli/command/image/build_buildkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ func runBuildBuildKit(dockerCli command.Cli, options buildOptions) error {
defer os.RemoveAll(dockerfileDir)
}

outputs, err := parseOutputs(options.outputs)
if err != nil {
return errors.Wrapf(err, "failed to parse outputs")
}

for _, out := range outputs {
if out.Type == "local" {
outDir, ok := out.Attrs["dest"]
if !ok {
return errors.Errorf("dest is required for local output")
}
delete(out.Attrs, "dest")
s.Allow(filesync.NewFSSyncTargetDir(outDir))
}
}

if dockerfileDir != "" {
s.Allow(filesync.NewFSSyncProvider([]filesync.SyncedDir{
{
Expand Down Expand Up @@ -182,6 +198,7 @@ func runBuildBuildKit(dockerCli command.Cli, options buildOptions) error {
buildOptions.RemoteContext = remote
buildOptions.SessionID = s.ID()
buildOptions.BuildID = buildID
buildOptions.Outputs = outputs
return doBuild(ctx, eg, dockerCli, options, buildOptions)
})

Expand Down

0 comments on commit 304e992

Please sign in to comment.