Skip to content

Commit

Permalink
fix: fixed an issue causing lambda package to hang in CI/CD
Browse files Browse the repository at this point in the history
  • Loading branch information
philasmar committed Nov 5, 2024
1 parent 5c8d1dc commit 4c905c0
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 24 deletions.
11 changes: 11 additions & 0 deletions .autover/changes/e52deae1-471a-4119-b874-5ef0ccf690b3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Projects": [
{
"Name": "Amazon.Lambda.Tools",
"Type": "Patch",
"ChangelogMessages": [
"Fixed an issue causing 'lambda package' to hang when ran in CI/CD. The fix applies for Linux-based systems."
]
}
]
}
96 changes: 72 additions & 24 deletions src/Amazon.Lambda.Tools/LambdaDotNetCLIWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,42 +184,71 @@ public int Publish(LambdaToolsDefaults defaults, string projectLocation, string

// echo the full dotnet command for debug
_logger?.WriteLine($"... dotnet {arguments}");

var psi = new ProcessStartInfo
{
FileName = dotnetCLI,
Arguments = arguments,
WorkingDirectory = this._workingDirectory,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};

int exitCode;
var handler = (DataReceivedEventHandler)((o, e) =>
{
if (string.IsNullOrEmpty(e.Data))
return;
_logger?.WriteLine("... publish: " + e.Data);
});

int exitCode;
using (var proc = new Process())
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
proc.StartInfo = psi;
proc.Start();
var psi = new ProcessStartInfo
{
FileName = dotnetCLI,
Arguments = arguments,
WorkingDirectory = this._workingDirectory,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};

using (var proc = new Process())
{
proc.StartInfo = psi;
proc.Start();


proc.ErrorDataReceived += handler;
proc.OutputDataReceived += handler;
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.ErrorDataReceived += handler;
proc.OutputDataReceived += handler;
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();

proc.EnableRaisingEvents = true;
proc.EnableRaisingEvents = true;

proc.WaitForExit();
proc.WaitForExit();

exitCode = proc.ExitCode;
exitCode = proc.ExitCode;
}
}
else
{
var tempOutputFile = Path.GetTempFileName();
var psi = new ProcessStartInfo
{
FileName = GetSystemShell(),
Arguments = $"-c \"{dotnetCLI} {arguments} > '{tempOutputFile}' 2>&1\"",
WorkingDirectory = this._workingDirectory,
UseShellExecute = true,
CreateNoWindow = true
};

using (var proc = new Process())
{
proc.StartInfo = psi;
proc.Start();

proc.EnableRaisingEvents = true;

proc.WaitForExit();

var output = File.ReadAllText(tempOutputFile);
_logger?.WriteLine(output);
File.Delete(tempOutputFile);

exitCode = proc.ExitCode;
}
}

if (exitCode == 0)
Expand Down Expand Up @@ -270,6 +299,25 @@ public int Publish(LambdaToolsDefaults defaults, string projectLocation, string

return exitCode;
}

private string GetSystemShell()
{
if (TryGetEnvironmentVariable("COMSPEC", out var comspec))
return comspec!;

if (TryGetEnvironmentVariable("SHELL", out var shell))
return shell!;

// fall back to defaults
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "/bin/sh";
}

private bool TryGetEnvironmentVariable(string variable, out string value)
{
value = Environment.GetEnvironmentVariable(variable);

return !string.IsNullOrEmpty(value);
}

public string GetPublishArguments(string projectLocation,
string outputLocation,
Expand Down

0 comments on commit 4c905c0

Please sign in to comment.