Skip to content

Commit

Permalink
bake: add sbom and provenance shorthands
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Chadwell <me@jedevc.com>
  • Loading branch information
jedevc committed Dec 5, 2022
1 parent bb43f5b commit 7e21294
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
9 changes: 9 additions & 0 deletions commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/containerd/containerd/platforms"
"github.com/docker/buildx/bake"
"github.com/docker/buildx/build"
"github.com/docker/buildx/util/buildflags"
"github.com/docker/buildx/util/confutil"
"github.com/docker/buildx/util/progress"
"github.com/docker/buildx/util/tracing"
Expand Down Expand Up @@ -71,6 +72,12 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions) (err error
if in.pull != nil {
overrides = append(overrides, fmt.Sprintf("*.pull=%t", *in.pull))
}
if in.sbom != "" {
overrides = append(overrides, fmt.Sprintf("*.attest=%s", buildflags.CanonicalizeAttest("sbom", in.sbom)))
}
if in.provenance != "" {
overrides = append(overrides, fmt.Sprintf("*.attest=%s", buildflags.CanonicalizeAttest("provenance", in.provenance)))
}
contextPathHash, _ := os.Getwd()

ctx2, cancel := context.WithCancel(context.TODO())
Expand Down Expand Up @@ -190,6 +197,8 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
flags.BoolVar(&options.exportLoad, "load", false, `Shorthand for "--set=*.output=type=docker"`)
flags.BoolVar(&options.printOnly, "print", false, "Print the options without building")
flags.BoolVar(&options.exportPush, "push", false, `Shorthand for "--set=*.output=type=registry"`)
flags.StringVar(&options.sbom, "sbom", "", `Shorthand for "--set=*.attest=type=sbom"`)
flags.StringVar(&options.provenance, "provenance", "", `Shorthand for "--set=*.attest=type=provenance"`)
flags.StringArrayVar(&options.overrides, "set", nil, `Override target value (e.g., "targetpattern.key=value")`)

commonBuildFlags(&options.commonOptions, flags)
Expand Down
5 changes: 3 additions & 2 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ type buildOptions struct {
noCacheFilter []string
outputs []string
platforms []string
provenance string
quiet bool
sbom string
secrets []string
shmSize dockeropts.MemBytes
ssh []string
Expand All @@ -86,6 +84,9 @@ type commonOptions struct {

exportPush bool
exportLoad bool

sbom string
provenance string
}

func runBuild(dockerCli command.Cli, in buildOptions) (err error) {
Expand Down
2 changes: 2 additions & 0 deletions docs/reference/buildx_bake.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ Build from a file
| [`--no-cache`](#no-cache) | | | Do not use cache when building the image |
| [`--print`](#print) | | | Print the options without building |
| [`--progress`](#progress) | `string` | `auto` | Set type of progress output (`auto`, `plain`, `tty`). Use plain to show container output |
| `--provenance` | `string` | | Shorthand for `--set=*.attest=type=provenance` |
| [`--pull`](#pull) | | | Always attempt to pull all referenced images |
| `--push` | | | Shorthand for `--set=*.output=type=registry` |
| `--sbom` | `string` | | Shorthand for `--set=*.attest=type=sbom` |
| [`--set`](#set) | `stringArray` | | Override target value (e.g., `targetpattern.key=value`) |


Expand Down
8 changes: 8 additions & 0 deletions util/buildflags/attests.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ package buildflags

import (
"encoding/csv"
"fmt"
"strconv"
"strings"

"github.com/pkg/errors"
)

func CanonicalizeAttest(attestType string, in string) string {
if b, err := strconv.ParseBool(in); err == nil && b {
return fmt.Sprintf("type=%s", attestType)
}
return fmt.Sprintf("type=%s,%s", attestType, in)
}

func ParseAttests(in []string) (map[string]string, error) {
out := map[string]string{}
for _, in := range in {
Expand Down

0 comments on commit 7e21294

Please sign in to comment.