diff --git a/src/Runner.Sdk/Util/IOUtil.cs b/src/Runner.Sdk/Util/IOUtil.cs index 1433e198b..da4a8a09b 100644 --- a/src/Runner.Sdk/Util/IOUtil.cs +++ b/src/Runner.Sdk/Util/IOUtil.cs @@ -6,6 +6,8 @@ using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading; +using System.Threading.Tasks; +using GitHub.Services.Common; namespace GitHub.Runner.Sdk { @@ -72,6 +74,25 @@ namespace GitHub.Runner.Sdk } } + public static async Task GetFileContentSha256HashAsync(string path) + { + if (!File.Exists(path)) + { + return string.Empty; + } + + using (FileStream stream = File.OpenRead(path)) + { + using (SHA256 sha256 = SHA256.Create()) + { + byte[] srcHashBytes = await sha256.ComputeHashAsync(stream); + var hash = PrimitiveExtensions.ConvertToHexString(srcHashBytes); + return hash; + } + + } + } + public static void Delete(string path, CancellationToken cancellationToken) { DeleteDirectory(path, cancellationToken); diff --git a/src/Runner.Worker/ActionManager.cs b/src/Runner.Worker/ActionManager.cs index 9e1366dbb..bf7838c1b 100644 --- a/src/Runner.Worker/ActionManager.cs +++ b/src/Runner.Worker/ActionManager.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; +using System.Security.Cryptography; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -855,11 +856,13 @@ namespace GitHub.Runner.Worker // tar -xzf using (var processInvoker = HostContext.CreateService()) { + var tarOutputs = new List(); processInvoker.OutputDataReceived += new EventHandler((sender, args) => { if (!string.IsNullOrEmpty(args.Data)) { Trace.Info(args.Data); + tarOutputs.Add($"STDOUT: {args.Data}"); } }); @@ -868,13 +871,23 @@ namespace GitHub.Runner.Worker if (!string.IsNullOrEmpty(args.Data)) { Trace.Error(args.Data); + tarOutputs.Add($"STDERR: {args.Data}"); } }); int exitCode = await processInvoker.ExecuteAsync(stagingDirectory, tar, $"-xzf \"{archiveFile}\"", null, executionContext.CancellationToken); if (exitCode != 0) { - throw new InvalidActionArchiveException($"Can't use 'tar -xzf' extract archive file: {archiveFile}. Action being checked out: {downloadInfo.NameWithOwner}@{downloadInfo.Ref}. return code: {exitCode}."); + if (executionContext.Global.Variables.GetBoolean("DistributedTask.DetailUntarFailure") == true) + { + var fileInfo = new FileInfo(archiveFile); + var sha256hash = await IOUtil.GetFileContentSha256HashAsync(archiveFile); + throw new InvalidActionArchiveException($"Can't use 'tar -xzf' extract archive file: {archiveFile} (SHA256 '{sha256hash}', size '{fileInfo.Length}' bytes, tar outputs '{string.Join(' ', tarOutputs)}'). Action being checked out: {downloadInfo.NameWithOwner}@{downloadInfo.Ref}. return code: {exitCode}."); + } + else + { + throw new InvalidActionArchiveException($"Can't use 'tar -xzf' extract archive file: {archiveFile}. Action being checked out: {downloadInfo.NameWithOwner}@{downloadInfo.Ref}. return code: {exitCode}."); + } } } #endif