Skip to content

Commit 96b2c25

Browse files
committed
fix issue with env's overwriting environment
1 parent 219852a commit 96b2c25

3 files changed

Lines changed: 39 additions & 10 deletions

File tree

src/Runner.Worker/Container/DockerCommandManager.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ public async Task<int> DockerBuild(IExecutionContext context, string workingDire
107107
public async Task<string> DockerCreate(IExecutionContext context, ContainerInfo container)
108108
{
109109
IList<string> dockerOptions = new List<string>();
110-
IDictionary<string, string> environment = new Dictionary<string, string>();
111110
// OPTIONS
112111
dockerOptions.Add($"--name {container.ContainerDisplayName}");
113112
dockerOptions.Add($"--label {DockerInstanceLabel}");
@@ -136,8 +135,7 @@ public async Task<string> DockerCreate(IExecutionContext context, ContainerInfo
136135
}
137136
else
138137
{
139-
environment.Add(env.Key, env.Value);
140-
dockerOptions.Add(DockerUtil.CreateEscapedOption("-e", env.Key));
138+
dockerOptions.Add(DockerUtil.CreateEscapedOption("-e", env.Key, env.Value));
141139
}
142140
}
143141

@@ -185,7 +183,7 @@ public async Task<string> DockerCreate(IExecutionContext context, ContainerInfo
185183
dockerOptions.Add($"{container.ContainerEntryPointArgs}");
186184

187185
var optionsString = string.Join(" ", dockerOptions);
188-
List<string> outputStrings = await ExecuteDockerCommandAsync(context, "create", optionsString, environment);
186+
List<string> outputStrings = await ExecuteDockerCommandAsync(context, "create", optionsString);
189187

190188
return outputStrings.FirstOrDefault();
191189
}
@@ -445,11 +443,6 @@ public Task<int> DockerLogin(IExecutionContext context, string configFileDirecto
445443
}
446444

447445
private async Task<List<string>> ExecuteDockerCommandAsync(IExecutionContext context, string command, string options)
448-
{
449-
return await ExecuteDockerCommandAsync(context, command, options, null);
450-
}
451-
452-
private async Task<List<string>> ExecuteDockerCommandAsync(IExecutionContext context, string command, string options, IDictionary<string, string> environment)
453446
{
454447
string arg = $"{command} {options}".Trim();
455448
context.Command($"{DockerPath} {arg}");
@@ -477,7 +470,7 @@ await processInvoker.ExecuteAsync(
477470
workingDirectory: context.GetGitHubContext("workspace"),
478471
fileName: DockerPath,
479472
arguments: arg,
480-
environment: environment,
473+
environment: null,
481474
requireExitCodeZero: true,
482475
outputEncoding: null,
483476
cancellationToken: CancellationToken.None);

src/Runner.Worker/Container/DockerUtil.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ public static string CreateEscapedOption(string flag, string key)
7171
return $"{flag} \"{EscapeString(key)}\"";
7272
}
7373

74+
public static string CreateEscapedOption(string flag, string key, string value)
75+
{
76+
if (String.IsNullOrEmpty(key))
77+
{
78+
return "";
79+
}
80+
return $"{flag} \"{EscapeString(key)}={value.Replace("\"", "\\\"")}\"";
81+
}
82+
7483
private static string EscapeString(string value)
7584
{
7685
return value.Replace("\\", "\\\\").Replace("\"", "\\\"");

src/Test/L0/Container/DockerUtilL0.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,32 @@ public void CreateEscapedOption_keyOnly(string input, string escaped)
171171
}
172172
Assert.Equal(expected, actual);
173173
}
174+
175+
[Theory]
176+
[Trait("Level", "L0")]
177+
[Trait("Category", "Worker")]
178+
[InlineData("HOME", "", "HOME", "")]
179+
[InlineData("HOME alpine:3.8 sh -c id #", "HOME alpine:3.8 sh -c id #", "HOME alpine:3.8 sh -c id #", "HOME alpine:3.8 sh -c id #")]
180+
[InlineData("HOME \"alpine:3.8 sh -c id #", "HOME \"alpine:3.8 sh -c id #", "HOME \\\"alpine:3.8 sh -c id #", "HOME \\\"alpine:3.8 sh -c id #")]
181+
[InlineData("HOME \\\"alpine:3.8 sh -c id #", "HOME \\\"alpine:3.8 sh -c id #", "HOME \\\\\\\"alpine:3.8 sh -c id #", "HOME \\\\\"alpine:3.8 sh -c id #")]
182+
[InlineData("HOME \\\\\"alpine:3.8 sh -c id #", "HOME \\\\\"alpine:3.8 sh -c id #", "HOME \\\\\\\\\\\"alpine:3.8 sh -c id #", "HOME \\\\\\\"alpine:3.8 sh -c id #")]
183+
[InlineData("HOME \"\"alpine:3.8 sh -c id #", "HOME \"\"alpine:3.8 sh -c id #", "HOME \\\"\\\"alpine:3.8 sh -c id #", "HOME \\\"\\\"alpine:3.8 sh -c id #")]
184+
[InlineData("HOME \\\"\"alpine:3.8 sh -c id #", "HOME \\\"\"alpine:3.8 sh -c id #", "HOME \\\\\\\"\\\"alpine:3.8 sh -c id #", "HOME \\\\\"\\\"alpine:3.8 sh -c id #")]
185+
[InlineData("HOME \"\\\"alpine:3.8 sh -c id #", "HOME \"\\\"alpine:3.8 sh -c id #", "HOME \\\"\\\\\\\"alpine:3.8 sh -c id #", "HOME \\\"\\\\\"alpine:3.8 sh -c id #")]
186+
public void CreateEscapedOption_keyValue(string keyInput, string valueInput, string escapedKey, string escapedValue)
187+
{
188+
var flag = "--example";
189+
var actual = DockerUtil.CreateEscapedOption(flag, keyInput, valueInput);
190+
string expected;
191+
if (String.IsNullOrEmpty(keyInput))
192+
{
193+
expected = "";
194+
}
195+
else
196+
{
197+
expected = $"{flag} \"{escapedKey}={escapedValue}\"";
198+
}
199+
Assert.Equal(expected, actual);
200+
}
174201
}
175202
}

0 commit comments

Comments
 (0)