Skip to content

Commit 535619d

Browse files
fix(build): ensure imageSpec.config.env variables are expanded (#6618)
Signed-off-by: Yaroslav Pershin <62902094+iapershin@users.noreply.github.com> Signed-off-by: Aleksei Igrychev <aleksei.igrychev@palark.com> Co-authored-by: Aleksei Igrychev <aleksei.igrychev@palark.com>
1 parent 2366801 commit 535619d

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

pkg/build/stage/image_spec.go

+43-11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/werf/common-go/pkg/util"
1212
"github.com/werf/werf/v2/pkg/config"
1313
"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"
1416
"github.com/werf/werf/v2/pkg/image"
1517
"github.com/werf/werf/v2/pkg/werf/global_warnings"
1618
)
@@ -60,7 +62,10 @@ func (s *ImageSpecStage) PrepareImage(ctx context.Context, _ Conveyor, _ contain
6062
}
6163

6264
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+
}
6469
newConfig.Volumes = modifyVolumes(ctx, imageInfo.Volumes, s.imageSpec.RemoveVolumes, s.imageSpec.Volumes)
6570

6671
if s.imageSpec.Expose != nil {
@@ -169,29 +174,43 @@ func modifyLabels(ctx context.Context, labels, addLabels map[string]string, remo
169174
return labels, nil
170175
}
171176

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))
174179
for _, entry := range env {
175180
parts := strings.SplitN(entry, "=", 2)
176181
if len(parts) == 2 {
177-
envMap[parts[0]] = parts[1]
182+
baseEnvMap[parts[0]] = parts[1]
178183
}
179184
}
180185

181186
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
183201
}
184202

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)
187206
}
188207

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))
192211
}
193212

194-
return result
213+
return resultEnv, nil
195214
}
196215

197216
func modifyVolumes(_ context.Context, volumes map[string]struct{}, removeVolumes, addVolumes []string) map[string]struct{} {
@@ -219,3 +238,16 @@ func sortSliceWithNewSlice(original []string) []string {
219238
func toDuration(seconds int) time.Duration {
220239
return time.Duration(seconds) * time.Second
221240
}
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

Comments
 (0)