Skip to content

Commit

Permalink
let container job handle prepend path correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
TingluoHuang committed Nov 16, 2018
1 parent 2f004a1 commit 7eb9a74
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Agent.Worker/Handlers/StepHost.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -33,6 +34,7 @@ Task<int> ExecuteAsync(string workingDirectory,
public interface IContainerStepHost : IStepHost
{
ContainerInfo Container { get; set; }
List<string> PrependPath { get; set; }
}

[ServiceLocator(Default = typeof(DefaultStepHost))]
Expand Down Expand Up @@ -79,6 +81,7 @@ public async Task<int> ExecuteAsync(string workingDirectory,
public sealed class ContainerStepHost : AgentService, IContainerStepHost
{
public ContainerInfo Container { get; set; }
public List<string> PrependPath { get; set; }
public event EventHandler<ProcessDataReceivedEventArgs> OutputDataReceived;
public event EventHandler<ProcessDataReceivedEventArgs> ErrorDataReceived;

Expand Down Expand Up @@ -137,6 +140,7 @@ public async Task<int> ExecuteAsync(string workingDirectory,
ExecutionHandlerWorkingDirectory = workingDirectory,
ExecutionHandlerArguments = arguments,
ExecutionHandlerEnvironment = environment,
ExecutionHandlerPrependPath = string.Join(Path.PathSeparator.ToString(), PrependPath.Reverse<string>())
};

// copy the intermediate script (containerHandlerInvoker.js) into Agent_TempDirectory
Expand Down Expand Up @@ -197,6 +201,9 @@ private class ContainerStandardInPayload

[JsonProperty("environment")]
public IDictionary<string, string> ExecutionHandlerEnvironment { get; set; }

[JsonProperty("prependPath")]
public string ExecutionHandlerPrependPath { get; set; }
}
}
}
1 change: 1 addition & 0 deletions src/Agent.Worker/TaskRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public async Task RunAsync()
ArgUtil.NotNullOrEmpty(ExecutionContext.Container.ContainerId, nameof(ExecutionContext.Container.ContainerId));
var containerStepHost = HostContext.CreateService<IContainerStepHost>();
containerStepHost.Container = ExecutionContext.Container;
containerStepHost.PrependPath = ExecutionContext.PrependPath;
stepHost = containerStepHost;
}
else
Expand Down
25 changes: 25 additions & 0 deletions src/Misc/layoutbin/containerHandlerInvoker.js.template
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ process.stdin.on('end', function () {
var handler = stdinData.handler;
var handlerArg = stdinData.args;
var handlerWorkDir = stdinData.workDir;
var prependPath = stdinData.prependPath;

console.log("##vso[task.debug]Handler: " + handler);
console.log("##vso[task.debug]HandlerArg: " + handlerArg);
Expand All @@ -18,13 +19,37 @@ process.stdin.on('end', function () {
process.env[key] = stdinData.environment[key];
});

var currentPath = process.env['PATH'];
var options = {
stdio: 'inherit',
cwd: handlerWorkDir
};
if (process.platform == 'win32') {
options.argv0 = `"${handler}"`;
options.windowsVerbatimArguments = true;

if (prependPath && prependPath.length > 0) {
if (currentPath && currentPath.length > 0) {
process.env['PATH'] = prependPath + ';' + currentPath;
}
else {
process.env['PATH'] = prependPath;
}
}
}
else {
if (prependPath && prependPath.length > 0) {
if (currentPath && currentPath.length > 0) {
process.env['PATH'] = prependPath + ':' + currentPath;
}
else {
process.env['PATH'] = prependPath;
}
}
}

if (prependPath && prependPath.length > 0) {
console.log("##vso[task.debug]Prepend Path: " + process.env['PATH']);
}

var launch = spawn(handler, [handlerArg], options);
Expand Down

0 comments on commit 7eb9a74

Please sign in to comment.