@@ -11,6 +11,8 @@ import (
11
11
"github.com/werf/common-go/pkg/util"
12
12
"github.com/werf/werf/v2/pkg/config"
13
13
"github.com/werf/werf/v2/pkg/container_backend"
14
+ "github.com/werf/werf/v2/pkg/dockerfile"
15
+ "github.com/werf/werf/v2/pkg/dockerfile/frontend"
14
16
"github.com/werf/werf/v2/pkg/image"
15
17
"github.com/werf/werf/v2/pkg/werf/global_warnings"
16
18
)
@@ -60,7 +62,10 @@ func (s *ImageSpecStage) PrepareImage(ctx context.Context, _ Conveyor, _ contain
60
62
}
61
63
62
64
newConfig .Labels = resultLabels
63
- newConfig .Env = modifyEnv (ctx , imageInfo .Env , s .imageSpec .RemoveEnv , s .imageSpec .Env )
65
+ newConfig .Env , err = modifyEnv (imageInfo .Env , s .imageSpec .RemoveEnv , s .imageSpec .Env )
66
+ if err != nil {
67
+ return fmt .Errorf ("unable to modify env: %w" , err )
68
+ }
64
69
newConfig .Volumes = modifyVolumes (ctx , imageInfo .Volumes , s .imageSpec .RemoveVolumes , s .imageSpec .Volumes )
65
70
66
71
if s .imageSpec .Expose != nil {
@@ -169,29 +174,43 @@ func modifyLabels(ctx context.Context, labels, addLabels map[string]string, remo
169
174
return labels , nil
170
175
}
171
176
172
- func modifyEnv (_ context. Context , env , removeKeys []string , addMap map [string ]string ) []string {
173
- envMap := make (map [string ]string , len (env ))
177
+ func modifyEnv (env , removeKeys []string , addKeysMap map [string ]string ) ( []string , error ) {
178
+ baseEnvMap := make (map [string ]string , len (env ))
174
179
for _ , entry := range env {
175
180
parts := strings .SplitN (entry , "=" , 2 )
176
181
if len (parts ) == 2 {
177
- envMap [parts [0 ]] = parts [1 ]
182
+ baseEnvMap [parts [0 ]] = parts [1 ]
178
183
}
179
184
}
180
185
181
186
for _ , key := range removeKeys {
182
- delete (envMap , key )
187
+ delete (baseEnvMap , key )
188
+ }
189
+
190
+ envMapToExpand := make (map [string ]string )
191
+ for key , value := range baseEnvMap {
192
+ envMapToExpand [key ] = value
193
+ }
194
+ for key , value := range addKeysMap {
195
+ // Do not add PATH to the baseEnvMap, because it is a special case for the expandEnv function.
196
+ if key != "PATH" {
197
+ baseEnvMap [key ] = value
198
+ }
199
+
200
+ envMapToExpand [key ] = value
183
201
}
184
202
185
- for key , value := range addMap {
186
- envMap [key ] = value
203
+ expandedEnvMap , err := expandEnv (baseEnvMap , envMapToExpand , dockerfile.ExpandOptions {})
204
+ if err != nil {
205
+ return nil , fmt .Errorf ("unable to expand env: %w" , err )
187
206
}
188
207
189
- result := make ([]string , 0 , len (envMap ))
190
- for key , value := range envMap {
191
- result = append (result , fmt .Sprintf ("%s=%s" , key , value ))
208
+ resultEnv := make ([]string , 0 , len (baseEnvMap ))
209
+ for key , value := range expandedEnvMap {
210
+ resultEnv = append (resultEnv , fmt .Sprintf ("%s=%s" , key , value ))
192
211
}
193
212
194
- return result
213
+ return resultEnv , nil
195
214
}
196
215
197
216
func modifyVolumes (_ context.Context , volumes map [string ]struct {}, removeVolumes , addVolumes []string ) map [string ]struct {} {
@@ -219,3 +238,16 @@ func sortSliceWithNewSlice(original []string) []string {
219
238
func toDuration (seconds int ) time.Duration {
220
239
return time .Duration (seconds ) * time .Second
221
240
}
241
+
242
+ func expandEnv (baseEnvMap , envMapToExpand map [string ]string , opts dockerfile.ExpandOptions ) (map [string ]string , error ) {
243
+ expander := frontend .NewShlexExpanderFactory ('\\' ).GetExpander (opts )
244
+ res := make (map [string ]string )
245
+ for k , v := range envMapToExpand {
246
+ newValue , err := expander .ProcessWordWithMap (v , baseEnvMap )
247
+ if err != nil {
248
+ return nil , fmt .Errorf ("error processing word %q: %w" , v , err )
249
+ }
250
+ res [k ] = newValue
251
+ }
252
+ return res , nil
253
+ }
0 commit comments