github.com/octohelm/wagon@v0.0.0-20240308040401-88662650dc0b/pkg/engine/plan/task/container_dockerfile.go (about) 1 package task 2 3 import ( 4 "context" 5 6 "github.com/octohelm/wagon/pkg/engine/plan" 7 8 "dagger.io/dagger" 9 "github.com/octohelm/wagon/pkg/engine/daggerutil" 10 "github.com/octohelm/wagon/pkg/engine/plan/task/core" 11 ) 12 13 func init() { 14 core.DefaultFactory.Register(&Dockerfile{}) 15 } 16 17 type Dockerfile struct { 18 core.Task 19 20 Source core.FS `json:"source"` 21 Dockerfile DockerfilePathOrContent `json:"dockerfile"` 22 Target string `json:"target,omitempty"` 23 BuildArg map[string]string `json:"buildArg"` 24 Label map[string]string `json:"label"` 25 Auth map[string]core.Auth `json:"auth"` 26 27 Platform string `json:"platform,omitempty" wagon:"generated,name=platform"` 28 Config core.ImageConfig `json:"-" wagon:"generated,name=config"` 29 Output core.FS `json:"-" wagon:"generated,name=output"` 30 31 Hosts map[string]string `json:"hosts" wagon:"deprecated"` 32 } 33 34 func (input *Dockerfile) Do(ctx context.Context) error { 35 return daggerutil.Do(ctx, func(c *dagger.Client) error { 36 dir := input.Source.LoadDirectory(c) 37 38 dockerfilePath := input.Dockerfile.Path 39 40 if contents := input.Dockerfile.Contents; contents != "" { 41 dockerfilePath = "/Dockerfile" 42 dir = dir.WithNewFile(dockerfilePath, contents) 43 } 44 45 buildOpts := dagger.ContainerBuildOpts{ 46 Dockerfile: dockerfilePath, 47 Target: input.Target, 48 } 49 50 for buildArg := range input.BuildArg { 51 buildArgValue := input.BuildArg[buildArg] 52 53 if buildArgValue == "" { 54 continue 55 } 56 57 buildOpts.BuildArgs = append(buildOpts.BuildArgs, dagger.BuildArg{ 58 Name: buildArg, 59 Value: buildArgValue, 60 }) 61 } 62 63 ct := c.Container(dagger.ContainerOpts{ 64 Platform: core.DefaultPlatform(input.Platform), 65 }) 66 67 ct = plan.RegistryAuthStoreContext.From(ctx).ApplyTo(ctx, ct) 68 for address := range input.Auth { 69 ct = input.Auth[address].ApplyTo(ctx, ct, address) 70 } 71 72 for label := range input.Label { 73 ct = ct.WithLabel(label, input.Label[label]) 74 } 75 76 ct = ct.Build(dir, buildOpts) 77 78 // hack to trigger Dockerfile build logs 79 _, err := ct.Rootfs().Entries(ctx) 80 if err != nil { 81 return err 82 } 83 84 id, err := ct.ID(ctx) 85 if err != nil { 86 return err 87 } 88 platform, err := ct.Platform(ctx) 89 if err != nil { 90 return err 91 } 92 93 if err := input.Config.Resolve(ctx, c, id); err != nil { 94 return err 95 } 96 97 input.Platform = string(platform) 98 return input.Output.SetDirectoryIDBy(ctx, ct.Rootfs()) 99 }) 100 } 101 102 type DockerfilePathOrContent struct { 103 Contents string `json:"contents,omitempty"` 104 Path string `json:"path" default:"Dockerfile"` 105 }