@@ -2,6 +2,10 @@ package custom
22
33import (
44 "fmt"
5+ "github.com/loft-sh/devspace/pkg/devspace/config"
6+ "github.com/loft-sh/devspace/pkg/devspace/config/loader/variable/runtime"
7+ "github.com/loft-sh/devspace/pkg/devspace/dependency/types"
8+ "github.com/loft-sh/devspace/pkg/util/shell"
59 "io"
610 "path/filepath"
711 "strings"
@@ -28,14 +32,20 @@ type Builder struct {
2832
2933 imageConfigName string
3034 imageTags []string
35+
36+ config config.Config
37+ dependencies []types.Dependency
3138}
3239
3340// NewBuilder creates a new custom builder
34- func NewBuilder (imageConfigName string , imageConf * latest.ImageConfig , imageTags []string ) * Builder {
41+ func NewBuilder (imageConfigName string , imageConf * latest.ImageConfig , imageTags []string , config config. Config , dependencies []types. Dependency ) * Builder {
3542 return & Builder {
3643 imageConfigName : imageConfigName ,
3744 imageConf : imageConf ,
3845 imageTags : imageTags ,
46+
47+ config : config ,
48+ dependencies : dependencies ,
3949 }
4050}
4151
@@ -74,7 +84,7 @@ func (b *Builder) ShouldRebuild(cache *generated.CacheConfig, forceRebuild bool,
7484 imageCache := cache .GetImageCache (b .imageConfigName )
7585
7686 // only rebuild Docker image when Dockerfile or context has changed since latest build
77- mustRebuild := imageCache .Tag == "" || imageCache .ImageConfigHash != imageConfigHash || imageCache .CustomFilesHash != customFilesHash
87+ mustRebuild := forceRebuild || b . imageConf . RebuildStrategy == latest . RebuildStrategyAlways || imageCache .Tag == "" || imageCache .ImageConfigHash != imageConfigHash || imageCache .CustomFilesHash != customFilesHash
7888
7989 imageCache .ImageConfigHash = imageConfigHash
8090 imageCache .CustomFilesHash = customFilesHash
@@ -87,8 +97,23 @@ func (b *Builder) Build(devspacePID string, log logpkg.Logger) error {
8797 // Build arguments
8898 args := []string {}
8999
90- // add args
91- args = append (args , b .imageConf .Build .Custom .Args ... )
100+ // resolve command
101+ if len (b .imageTags ) > 0 {
102+ key := fmt .Sprintf ("images.%s" , b .imageConfigName )
103+ b .config .SetRuntimeVariable (key , b .imageConf .Image + ":" + b .imageTags [0 ])
104+ b .config .SetRuntimeVariable (key + ".image" , b .imageConf .Image )
105+ b .config .SetRuntimeVariable (key + ".tag" , b .imageTags [0 ])
106+ }
107+
108+ // loop over args
109+ for i := range b .imageConf .Build .Custom .Args {
110+ resolvedArg , err := runtime .NewRuntimeResolver (false ).FillRuntimeVariablesAsString (b .imageConf .Build .Custom .Args [i ], b .config , b .dependencies )
111+ if err != nil {
112+ return err
113+ }
114+
115+ args = append (args , resolvedArg )
116+ }
92117
93118 // add image arg
94119 if ! b .imageConf .Build .Custom .SkipImageArg {
@@ -106,7 +131,14 @@ func (b *Builder) Build(devspacePID string, log logpkg.Logger) error {
106131 }
107132
108133 // append the rest
109- args = append (args , b .imageConf .Build .Custom .AppendArgs ... )
134+ for i := range b .imageConf .Build .Custom .AppendArgs {
135+ resolvedArg , err := runtime .NewRuntimeResolver (false ).FillRuntimeVariablesAsString (b .imageConf .Build .Custom .AppendArgs [i ], b .config , b .dependencies )
136+ if err != nil {
137+ return err
138+ }
139+
140+ args = append (args , resolvedArg )
141+ }
110142
111143 // get the command
112144 commandPath := b .imageConf .Build .Custom .Command
@@ -122,11 +154,11 @@ func (b *Builder) Build(devspacePID string, log logpkg.Logger) error {
122154 return fmt .Errorf ("no command specified for custom builder" )
123155 }
124156
125- // make sure the path has the correct slashes
126- commandPath = filepath . FromSlash ( commandPath )
127-
128- // Create the command
129- cmd := command . NewStreamCommand ( commandPath , args )
157+ // resolve command and args
158+ commandPath , err := runtime . NewRuntimeResolver ( false ). FillRuntimeVariablesAsString ( commandPath , b . config , b . dependencies )
159+ if err != nil {
160+ return err
161+ }
130162
131163 // Determine output writer
132164 var writer io.Writer
@@ -138,9 +170,16 @@ func (b *Builder) Build(devspacePID string, log logpkg.Logger) error {
138170
139171 log .Infof ("Build %s:%s with custom command '%s %s'" , b .imageConf .Image , b .imageTags [0 ], commandPath , strings .Join (args , " " ))
140172
141- err := cmd .Run (writer , writer , nil )
142- if err != nil {
143- return errors .Errorf ("Error building image: %v" , err )
173+ if len (args ) == 0 {
174+ err = shell .ExecuteShellCommand (commandPath , args , filepath .Dir (b .config .Path ()), writer , writer , nil )
175+ if err != nil {
176+ return errors .Errorf ("error building image: %v" , err )
177+ }
178+ } else {
179+ err = command .NewStreamCommand (commandPath , args ).Run (writer , writer , nil )
180+ if err != nil {
181+ return errors .Errorf ("error building image: %v" , err )
182+ }
144183 }
145184
146185 log .Done ("Done processing image '" + b .imageConf .Image + "'" )
0 commit comments