Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 2218859

Browse files
authored
Merge pull request #768 from N4rm0/fix-envar-replacement-for-url
Fix #691 - ADD does not understand ENV variables
2 parents e87304d + 019b26e commit 2218859

4 files changed

Lines changed: 27 additions & 11 deletions

File tree

integration/dockerfiles/Dockerfile_test_add

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ ADD https://github.com/GoogleCloudPlatform/docker-credential-gcr/releases/downlo
2828
# Test environment replacement in the URL
2929
ENV VERSION=v1.4.3
3030
ADD https://github.com/GoogleCloudPlatform/docker-credential-gcr/releases/download/${VERSION}-static/docker-credential-gcr_linux_amd64-1.4.3.tar.gz /destination
31+
32+
# Test full url replacement
33+
ENV URL=https://github.com/GoogleCloudPlatform/docker-credential-gcr/releases/download/v1.4.3/docker-credential-gcr_linux_386-1.4.3.tar.gz
34+
ADD $URL /otherdestination

integration/integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ func TestGitBuildContextWithBranch(t *testing.T) {
323323

324324
func TestLayers(t *testing.T) {
325325
offset := map[string]int{
326-
"Dockerfile_test_add": 11,
326+
"Dockerfile_test_add": 12,
327327
"Dockerfile_test_scratch": 3,
328328
}
329329
for dockerfile := range imageBuilder.FilesBuilt {

pkg/util/command_util.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,7 @@ import (
3737
func ResolveEnvironmentReplacementList(values, envs []string, isFilepath bool) ([]string, error) {
3838
var resolvedValues []string
3939
for _, value := range values {
40-
var resolved string
41-
var err error
42-
if IsSrcRemoteFileURL(value) {
43-
resolved, err = ResolveEnvironmentReplacement(value, envs, false)
44-
} else {
45-
resolved, err = ResolveEnvironmentReplacement(value, envs, isFilepath)
46-
}
40+
resolved, err := ResolveEnvironmentReplacement(value, envs, isFilepath)
4741
logrus.Debugf("Resolved %s to %s", value, resolved)
4842
if err != nil {
4943
return nil, err
@@ -65,7 +59,8 @@ func ResolveEnvironmentReplacementList(values, envs []string, isFilepath bool) (
6559
func ResolveEnvironmentReplacement(value string, envs []string, isFilepath bool) (string, error) {
6660
shlex := shell.NewLex(parser.DefaultEscapeToken)
6761
fp, err := shlex.ProcessWord(value, envs)
68-
if !isFilepath {
62+
// Check after replacement if value is a remote URL
63+
if !isFilepath || IsSrcRemoteFileURL(fp) {
6964
return fp, err
7065
}
7166
if err != nil {

pkg/util/command_util_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ var testEnvReplacement = []struct {
9797
},
9898
expectedPath: "8080/udp",
9999
},
100+
{
101+
path: "$url",
102+
envs: []string{
103+
"url=http://example.com",
104+
},
105+
isFilepath: true,
106+
expectedPath: "http://example.com",
107+
},
108+
{
109+
path: "$url",
110+
envs: []string{
111+
"url=http://example.com",
112+
},
113+
isFilepath: false,
114+
expectedPath: "http://example.com",
115+
},
100116
}
101117

102118
func Test_EnvReplacement(t *testing.T) {
@@ -472,14 +488,15 @@ func TestResolveEnvironmentReplacementList(t *testing.T) {
472488
name: "url",
473489
args: args{
474490
values: []string{
475-
"https://google.com/$foo", "$bar",
491+
"https://google.com/$foo", "$bar", "$url",
476492
},
477493
envs: []string{
478494
"foo=baz",
479495
"bar=bat",
496+
"url=https://google.com",
480497
},
481498
},
482-
want: []string{"https://google.com/baz", "bat"},
499+
want: []string{"https://google.com/baz", "bat", "https://google.com"},
483500
},
484501
{
485502
name: "mixed",

0 commit comments

Comments
 (0)