Cleanup FileCommands (#693)

This commit is contained in:
Thomas Boop
2020-09-04 15:35:36 -04:00
committed by TingluoHuang
parent d74c400c38
commit 9d7b633a5b
3 changed files with 33 additions and 24 deletions

View File

@@ -147,9 +147,7 @@ namespace GitHub.Runner.Worker
// Setup File Command Manager
var fileCommandManager = HostContext.CreateService<IFileCommandManager>();
// Container Action Handler will handle the conversion for Container Actions
var container = handlerData.ExecutionType == ActionExecutionType.Container ? null : ExecutionContext.Global.Container;
fileCommandManager.InitializeFiles(ExecutionContext, container);
fileCommandManager.InitializeFiles(ExecutionContext, null);
// Load the inputs.
ExecutionContext.Debug("Loading inputs");
@@ -244,8 +242,14 @@ namespace GitHub.Runner.Worker
handler.PrintActionDetails(Stage);
// Run the task.
try
{
await handler.RunAsync(Stage);
fileCommandManager.TryProcessFiles(ExecutionContext, ExecutionContext.Global.Container);
}
finally
{
fileCommandManager.ProcessFiles(ExecutionContext, ExecutionContext.Global.Container);
}
}

View File

@@ -1,11 +1,12 @@
using GitHub.Runner.Worker.Container;
using System;
using System.Text;
using System.IO;
using GitHub.DistributedTask.WebApi;
using GitHub.Runner.Worker.Container;
using GitHub.Runner.Common;
using GitHub.Runner.Sdk;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace GitHub.Runner.Worker
{
@@ -13,7 +14,7 @@ namespace GitHub.Runner.Worker
public interface IFileCommandManager : IRunnerService
{
void InitializeFiles(IExecutionContext context, ContainerInfo container);
void TryProcessFiles(IExecutionContext context, ContainerInfo container);
void ProcessFiles(IExecutionContext context, ContainerInfo container);
}
@@ -46,13 +47,13 @@ namespace GitHub.Runner.Worker
_fileSuffix = Guid.NewGuid().ToString();
foreach (var fileCommand in _commandExtensions)
{
var oldPath = Path.Combine(_fileCommandDirectory, fileCommand.FileName + oldSuffix);
var oldPath = Path.Combine(_fileCommandDirectory, fileCommand.FilePrefix + oldSuffix);
if (oldSuffix != String.Empty && File.Exists(oldPath))
{
TryDeleteFile(oldPath);
}
var newPath = Path.Combine(_fileCommandDirectory, fileCommand.FileName + _fileSuffix);
var newPath = Path.Combine(_fileCommandDirectory, fileCommand.FilePrefix + _fileSuffix);
TryDeleteFile(newPath);
File.Create(newPath).Dispose();
@@ -61,11 +62,20 @@ namespace GitHub.Runner.Worker
}
}
public void TryProcessFiles(IExecutionContext context, ContainerInfo container)
public void ProcessFiles(IExecutionContext context, ContainerInfo container)
{
foreach (var fileCommand in _commandExtensions)
{
fileCommand.ProcessCommand(context, Path.Combine(_fileCommandDirectory, fileCommand.FileName + _fileSuffix),container);
try
{
fileCommand.ProcessCommand(context, Path.Combine(_fileCommandDirectory, fileCommand.FilePrefix + _fileSuffix),container);
}
catch (Exception ex)
{
context.Error($"Unable to process file command '{fileCommand.ContextName}' successfully.");
context.Error(ex);
context.CommandResult = TaskResult.Failed;
}
}
}
@@ -91,7 +101,7 @@ namespace GitHub.Runner.Worker
public interface IFileCommandExtension : IExtension
{
string ContextName { get; }
string FileName { get; }
string FilePrefix { get; }
void ProcessCommand(IExecutionContext context, string filePath, ContainerInfo container);
}
@@ -99,7 +109,7 @@ namespace GitHub.Runner.Worker
public sealed class AddPathFileCommand : RunnerService, IFileCommandExtension
{
public string ContextName => "path";
public string FileName => "add_path_";
public string FilePrefix => "add_path_";
public Type ExtensionType => typeof(IFileCommandExtension);
@@ -124,7 +134,7 @@ namespace GitHub.Runner.Worker
public sealed class SetEnvFileCommand : RunnerService, IFileCommandExtension
{
public string ContextName => "env";
public string FileName => "set_env_";
public string FilePrefix => "set_env_";
public Type ExtensionType => typeof(IFileCommandExtension);
@@ -195,11 +205,6 @@ namespace GitHub.Runner.Worker
{
context.Debug($"Environment variables file does not exist '{filePath}'");
}
catch (Exception ex)
{
context.Error($"Failed to read environment variables file '{filePath}'");
context.Error(ex);
}
}
private static void SetEnvironmentVariable(

View File

@@ -6,7 +6,7 @@ namespace GitHub.Runner.Worker
{
public sealed class GitHubContext : DictionaryContextData, IEnvironmentContextData
{
private readonly HashSet<string> _contextEnvWhitelist = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
private readonly HashSet<string> _contextEnvAllowlist = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"action",
"action_path",
@@ -35,7 +35,7 @@ namespace GitHub.Runner.Worker
{
foreach (var data in this)
{
if (_contextEnvWhitelist.Contains(data.Key) && data.Value is StringContextData value)
if (_contextEnvAllowlist.Contains(data.Key) && data.Value is StringContextData value)
{
yield return new KeyValuePair<string, string>($"GITHUB_{data.Key.ToUpperInvariant()}", value);
}