Skip to content

Commit b729abf

Browse files
Merge pull request #323 from depot/billy/feat/custom-tags
feat: add support for custom save tags
2 parents aaa81d1 + d5e9109 commit b729abf

File tree

5 files changed

+493
-440
lines changed

5 files changed

+493
-440
lines changed

pkg/buildx/commands/bake.go

+48-6
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func RunBake(dockerCli command.Cli, in BakeOptions, validator BakeValidator, pri
203203
_ = printer.Wait()
204204

205205
if in.save {
206-
printSaveHelp(in.project, in.buildID, in.progress, requestedTargets)
206+
printSaveHelp(in.project, in.buildID, in.progress, requestedTargets, in.additionalTags)
207207
}
208208
linter.Print(os.Stderr, in.progress)
209209
return nil
@@ -295,10 +295,11 @@ func BakeCmd() *cobra.Command {
295295
options.project,
296296
bakeOpts,
297297
helpers.UsingDepotFeatures{
298-
Push: options.exportPush,
299-
Load: options.exportLoad,
300-
Save: options.save,
301-
Lint: options.lint,
298+
Push: options.exportPush,
299+
Load: options.exportLoad,
300+
Save: options.save,
301+
SaveTags: options.saveTags,
302+
Lint: options.lint,
302303
},
303304
)
304305
build, err := helpers.BeginBuild(context.Background(), req, token)
@@ -557,7 +558,7 @@ func parseBakeTargets(targets []string) (bkt bakeTargets) {
557558
}
558559

559560
// printSaveHelp prints instructions to pull or push the saved targets.
560-
func printSaveHelp(project, buildID, progressMode string, requestedTargets []string) {
561+
func printSaveHelp(project, buildID, progressMode string, requestedTargets, additionalTags []string) {
561562
if progressMode != progress.PrinterModeQuiet {
562563
fmt.Fprintln(os.Stderr)
563564
saved := "target"
@@ -573,6 +574,47 @@ func printSaveHelp(project, buildID, progressMode string, requestedTargets []str
573574
targets := strings.Join(requestedTargets, ",")
574575
fmt.Fprintf(os.Stderr, "Saved %s: %s\n", saved, targets)
575576
fmt.Fprintf(os.Stderr, "\tTo pull: depot pull --project %s %s\n", project, buildID)
577+
578+
if len(additionalTags) > 1 {
579+
fmt.Fprintf(os.Stderr, "\tTo pull save-tags:\n")
580+
fmt.Fprintf(os.Stderr, "\t\tdocker login registry.depot.dev -u x-token -p $(depot pull-token)\n")
581+
fmt.Fprintln(os.Stderr)
582+
583+
// the api will send multiple of the same tag back for each target
584+
if len(requestedTargets) > 0 {
585+
seenTags := map[string]struct{}{}
586+
for _, target := range requestedTargets {
587+
if target == "default" {
588+
continue
589+
}
590+
591+
for _, tag := range additionalTags {
592+
if strings.Contains(tag, buildID) {
593+
continue
594+
}
595+
596+
trueTag := tag + "-" + target
597+
if _, ok := seenTags[trueTag]; ok {
598+
continue
599+
}
600+
seenTags[trueTag] = struct{}{}
601+
602+
fmt.Fprintf(os.Stderr, "\t\tdocker pull %s\n", trueTag)
603+
}
604+
}
605+
} else {
606+
for _, tag := range additionalTags {
607+
if strings.Contains(tag, buildID) {
608+
continue
609+
}
610+
611+
fmt.Fprintf(os.Stderr, "\t\tdocker pull %s\n", tag)
612+
}
613+
}
614+
615+
fmt.Fprintln(os.Stderr)
616+
}
617+
576618
fmt.Fprintf(os.Stderr, "\tTo push: depot push %s--project %s --tag <REPOSITORY:TAG> %s\n", targetUsage, project, buildID)
577619
}
578620
}

pkg/buildx/commands/build.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ type DepotOptions struct {
120120
build *depotbuild.Build
121121

122122
save bool
123+
saveTags []string
123124
additionalTags []string
124125
additionalCredentials []depotbuild.Credential
125126

@@ -353,7 +354,7 @@ func buildTargets(ctx context.Context, dockerCli command.Cli, nodes []builder.No
353354

354355
printWarnings(os.Stderr, printer.Warnings(), progressMode)
355356
if depotOpts.save {
356-
printSaveHelp(depotOpts.project, depotOpts.buildID, progressMode, nil)
357+
printSaveHelp(depotOpts.project, depotOpts.buildID, progressMode, nil, depotOpts.additionalTags)
357358
}
358359
linter.Print(os.Stderr, progressMode)
359360

@@ -647,10 +648,11 @@ func BuildCmd() *cobra.Command {
647648
options.project,
648649
validatedOpts,
649650
helpers.UsingDepotFeatures{
650-
Push: options.exportPush,
651-
Load: options.exportLoad,
652-
Save: options.save,
653-
Lint: options.lint,
651+
Push: options.exportPush,
652+
Load: options.exportLoad,
653+
Save: options.save,
654+
SaveTags: options.saveTags,
655+
Lint: options.lint,
654656
},
655657
)
656658

@@ -867,6 +869,7 @@ func depotAttestationFlags(_ *cobra.Command, options *DepotOptions, flags *pflag
867869

868870
func depotRegistryFlags(_ *cobra.Command, options *DepotOptions, flags *pflag.FlagSet) {
869871
flags.BoolVar(&options.save, "save", false, `Saves the build to the depot registry`)
872+
flags.StringArrayVar(&options.saveTags, "save-tag", []string{}, `Additional custom tag for the saved image, use with --save`)
870873
}
871874

872875
func checkWarnedFlags(f *pflag.Flag) {

pkg/helpers/build.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ func BeginBuild(ctx context.Context, req *cliv1.CreateBuildRequest, token string
3939
}
4040

4141
type UsingDepotFeatures struct {
42-
Push bool
43-
Load bool
44-
Save bool
45-
Lint bool
42+
Push bool
43+
Load bool
44+
Save bool
45+
SaveTags []string
46+
Lint bool
4647
}
4748

4849
func NewBuildRequest(project string, opts map[string]buildx.Options, features UsingDepotFeatures) *cliv1.CreateBuildRequest {
@@ -67,6 +68,7 @@ func NewBuildRequest(project string, opts map[string]buildx.Options, features Us
6768
{
6869
Command: cliv1.Command_COMMAND_BUILD,
6970
Tags: opts.Tags,
71+
SaveTags: features.SaveTags,
7072
Outputs: outputs,
7173
Push: features.Push,
7274
Load: features.Load,
@@ -84,8 +86,8 @@ func NewBuildRequest(project string, opts map[string]buildx.Options, features Us
8486
func NewBakeRequest(project string, opts map[string]buildx.Options, features UsingDepotFeatures) *cliv1.CreateBuildRequest {
8587
targets := make([]*cliv1.BuildOptions, 0, len(opts))
8688

87-
for name, opts := range opts {
88-
name := name
89+
for targetName, opts := range opts {
90+
targetName := targetName
8991
outputs := make([]*cliv1.BuildOutput, len(opts.Exports))
9092
for i := range opts.Exports {
9193
outputs[i] = &cliv1.BuildOutput{
@@ -102,7 +104,8 @@ func NewBakeRequest(project string, opts map[string]buildx.Options, features Usi
102104
Load: features.Load,
103105
Save: features.Save,
104106
Lint: features.Lint,
105-
TargetName: &name,
107+
SaveTags: features.SaveTags,
108+
TargetName: &targetName,
106109
})
107110
}
108111

0 commit comments

Comments
 (0)