diff --git a/src/Runner.Worker/ActionRunner.cs b/src/Runner.Worker/ActionRunner.cs index f946573e4..d9387286a 100644 --- a/src/Runner.Worker/ActionRunner.cs +++ b/src/Runner.Worker/ActionRunner.cs @@ -147,9 +147,7 @@ namespace GitHub.Runner.Worker // Setup File Command Manager var fileCommandManager = HostContext.CreateService(); - // 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. - await handler.RunAsync(Stage); - fileCommandManager.TryProcessFiles(ExecutionContext, ExecutionContext.Global.Container); + try + { + await handler.RunAsync(Stage); + } + finally + { + fileCommandManager.ProcessFiles(ExecutionContext, ExecutionContext.Global.Container); + } } diff --git a/src/Runner.Worker/FileCommandManager.cs b/src/Runner.Worker/FileCommandManager.cs index b6251112c..aea76f10a 100644 --- a/src/Runner.Worker/FileCommandManager.cs +++ b/src/Runner.Worker/FileCommandManager.cs @@ -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( diff --git a/src/Runner.Worker/GitHubContext.cs b/src/Runner.Worker/GitHubContext.cs index 4eb98f770..5079206cf 100644 --- a/src/Runner.Worker/GitHubContext.cs +++ b/src/Runner.Worker/GitHubContext.cs @@ -6,7 +6,7 @@ namespace GitHub.Runner.Worker { public sealed class GitHubContext : DictionaryContextData, IEnvironmentContextData { - private readonly HashSet _contextEnvWhitelist = new HashSet(StringComparer.OrdinalIgnoreCase) + private readonly HashSet _contextEnvAllowlist = new HashSet(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($"GITHUB_{data.Key.ToUpperInvariant()}", value); }