|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using GitHub.DistributedTask.Pipelines; |
| 8 | +using GitHub.DistributedTask.Pipelines.ContextData; |
| 9 | +using GitHub.DistributedTask.WebApi; |
| 10 | +using GitHub.Runner.Common; |
| 11 | +using GitHub.Runner.Common.Util; |
| 12 | +using GitHub.Runner.Sdk; |
| 13 | +using GitHub.Runner.Worker.Container; |
| 14 | +using GitHub.Runner.Worker.Container.ContainerHooks; |
| 15 | + |
| 16 | +namespace GitHub.Runner.Worker.Handlers |
| 17 | +{ |
| 18 | + [ServiceLocator(Default = typeof(GoActionHandler))] |
| 19 | + public interface IGoActionHandler : IHandler |
| 20 | + { |
| 21 | + GoActionExecutionData Data { get; set; } |
| 22 | + } |
| 23 | + |
| 24 | + public sealed class GoActionHandler : Handler, IGoActionHandler |
| 25 | + { |
| 26 | + public GoActionExecutionData Data { get; set; } |
| 27 | + |
| 28 | + public async Task RunAsync(ActionRunStage stage) |
| 29 | + { |
| 30 | + // Validate args. |
| 31 | + Trace.Entering(); |
| 32 | + ArgUtil.NotNull(Data, nameof(Data)); |
| 33 | + ArgUtil.NotNull(ExecutionContext, nameof(ExecutionContext)); |
| 34 | + ArgUtil.NotNull(Inputs, nameof(Inputs)); |
| 35 | + ArgUtil.Directory(ActionDirectory, nameof(ActionDirectory)); |
| 36 | + |
| 37 | + // Update the env dictionary. |
| 38 | + AddInputsToEnvironment(); |
| 39 | + AddPrependPathToEnvironment(); |
| 40 | + |
| 41 | + // expose context to environment |
| 42 | + foreach (var context in ExecutionContext.ExpressionValues) |
| 43 | + { |
| 44 | + if (context.Value is IEnvironmentContextData runtimeContext && runtimeContext != null) |
| 45 | + { |
| 46 | + foreach (var env in runtimeContext.GetRuntimeEnvironmentVariables()) |
| 47 | + { |
| 48 | + Environment[env.Key] = env.Value; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // Add Actions Runtime server info |
| 54 | + var systemConnection = ExecutionContext.Global.Endpoints.Single(x => string.Equals(x.Name, WellKnownServiceEndpointNames.SystemVssConnection, StringComparison.OrdinalIgnoreCase)); |
| 55 | + Environment["ACTIONS_RUNTIME_URL"] = systemConnection.Url.AbsoluteUri; |
| 56 | + Environment["ACTIONS_RUNTIME_TOKEN"] = systemConnection.Authorization.Parameters[EndpointAuthorizationParameters.AccessToken]; |
| 57 | + if (systemConnection.Data.TryGetValue("CacheServerUrl", out var cacheUrl) && !string.IsNullOrEmpty(cacheUrl)) |
| 58 | + { |
| 59 | + Environment["ACTIONS_CACHE_URL"] = cacheUrl; |
| 60 | + } |
| 61 | + if (systemConnection.Data.TryGetValue("PipelinesServiceUrl", out var pipelinesServiceUrl) && !string.IsNullOrEmpty(pipelinesServiceUrl)) |
| 62 | + { |
| 63 | + Environment["ACTIONS_RUNTIME_URL"] = pipelinesServiceUrl; |
| 64 | + } |
| 65 | + if (systemConnection.Data.TryGetValue("GenerateIdTokenUrl", out var generateIdTokenUrl) && !string.IsNullOrEmpty(generateIdTokenUrl)) |
| 66 | + { |
| 67 | + Environment["ACTIONS_ID_TOKEN_REQUEST_URL"] = generateIdTokenUrl; |
| 68 | + Environment["ACTIONS_ID_TOKEN_REQUEST_TOKEN"] = systemConnection.Authorization.Parameters[EndpointAuthorizationParameters.AccessToken]; |
| 69 | + } |
| 70 | + if (systemConnection.Data.TryGetValue("ResultsServiceUrl", out var resultsUrl) && !string.IsNullOrEmpty(resultsUrl)) |
| 71 | + { |
| 72 | + Environment["ACTIONS_RESULTS_URL"] = resultsUrl; |
| 73 | + } |
| 74 | + |
| 75 | + if (ExecutionContext.Global.Variables.GetBoolean("actions_uses_cache_service_v2") ?? false) |
| 76 | + { |
| 77 | + Environment["ACTIONS_CACHE_SERVICE_V2"] = bool.TrueString; |
| 78 | + } |
| 79 | + |
| 80 | + // Resolve the target script. |
| 81 | + string target = null; |
| 82 | + if (stage == ActionRunStage.Main) |
| 83 | + { |
| 84 | + target = Data.Main; |
| 85 | + } |
| 86 | + else if (stage == ActionRunStage.Pre) |
| 87 | + { |
| 88 | + target = Data.Pre; |
| 89 | + } |
| 90 | + else if (stage == ActionRunStage.Post) |
| 91 | + { |
| 92 | + target = Data.Post; |
| 93 | + } |
| 94 | + |
| 95 | + // Set extra telemetry base on the current context. |
| 96 | + if (stage == ActionRunStage.Main) |
| 97 | + { |
| 98 | + ExecutionContext.StepTelemetry.HasPreStep = Data.HasPre; |
| 99 | + ExecutionContext.StepTelemetry.HasPostStep = Data.HasPost; |
| 100 | + } |
| 101 | + |
| 102 | + ArgUtil.NotNullOrEmpty(target, nameof(target)); |
| 103 | + var targetBaseName = target; |
| 104 | + target = Path.Combine(ActionDirectory, target + ".out"); |
| 105 | + |
| 106 | + // Resolve the working directory. |
| 107 | + string workingDirectory = ExecutionContext.GetGitHubContext("workspace"); |
| 108 | + if (string.IsNullOrEmpty(workingDirectory)) |
| 109 | + { |
| 110 | + workingDirectory = HostContext.GetDirectory(WellKnownDirectory.Work); |
| 111 | + } |
| 112 | + |
| 113 | + // It appears that node.exe outputs UTF8 when not in TTY mode. |
| 114 | + Encoding outputEncoding = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows) ? Encoding.UTF8 : null; |
| 115 | + |
| 116 | + using (var stdoutManager = new OutputManager(ExecutionContext, ActionCommandManager)) |
| 117 | + using (var stderrManager = new OutputManager(ExecutionContext, ActionCommandManager)) |
| 118 | + { |
| 119 | + StepHost.OutputDataReceived += stdoutManager.OnDataReceived; |
| 120 | + StepHost.ErrorDataReceived += stderrManager.OnDataReceived; |
| 121 | + |
| 122 | + await StepHost.ExecuteAsync(ExecutionContext, |
| 123 | + workingDirectory: StepHost.ResolvePathForStepHost(ExecutionContext, ActionDirectory), |
| 124 | + fileName: StepHost.ResolvePathForStepHost(ExecutionContext, "go"), |
| 125 | + arguments: $"build -o {targetBaseName}.out {targetBaseName}", |
| 126 | + environment: Environment, |
| 127 | + requireExitCodeZero: false, |
| 128 | + outputEncoding: outputEncoding, |
| 129 | + killProcessOnCancel: false, |
| 130 | + inheritConsoleHandler: !ExecutionContext.Global.Variables.Retain_Default_Encoding, |
| 131 | + standardInInput: null, |
| 132 | + cancellationToken: ExecutionContext.CancellationToken); |
| 133 | + |
| 134 | + ArgUtil.File(target, nameof(target)); |
| 135 | + |
| 136 | + // Execute the process. Exit code 0 should always be returned. |
| 137 | + // A non-zero exit code indicates infrastructural failure. |
| 138 | + // Task failure should be communicated over STDOUT using ## commands. |
| 139 | + Task<int> step = StepHost.ExecuteAsync(ExecutionContext, |
| 140 | + workingDirectory: StepHost.ResolvePathForStepHost(ExecutionContext, workingDirectory), |
| 141 | + fileName: StepHost.ResolvePathForStepHost(ExecutionContext, target), |
| 142 | + arguments: string.Empty, |
| 143 | + environment: Environment, |
| 144 | + requireExitCodeZero: false, |
| 145 | + outputEncoding: outputEncoding, |
| 146 | + killProcessOnCancel: false, |
| 147 | + inheritConsoleHandler: !ExecutionContext.Global.Variables.Retain_Default_Encoding, |
| 148 | + standardInInput: null, |
| 149 | + cancellationToken: ExecutionContext.CancellationToken); |
| 150 | + |
| 151 | + // Wait for either the node exit or force finish through ##vso command |
| 152 | + await System.Threading.Tasks.Task.WhenAny(step, ExecutionContext.ForceCompleted); |
| 153 | + |
| 154 | + if (ExecutionContext.ForceCompleted.IsCompleted) |
| 155 | + { |
| 156 | + ExecutionContext.Debug("The task was marked as \"done\", but the process has not closed after 5 seconds. Treating the task as complete."); |
| 157 | + } |
| 158 | + else |
| 159 | + { |
| 160 | + var exitCode = await step; |
| 161 | + ExecutionContext.Debug($"Go Action run completed with exit code {exitCode}"); |
| 162 | + if (exitCode != 0) |
| 163 | + { |
| 164 | + ExecutionContext.Result = TaskResult.Failed; |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments