Skip to content

Commit 4c905c0

Browse files
committed
fix: fixed an issue causing lambda package to hang in CI/CD
1 parent 5c8d1dc commit 4c905c0

File tree

2 files changed

+83
-24
lines changed

2 files changed

+83
-24
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"Projects": [
3+
{
4+
"Name": "Amazon.Lambda.Tools",
5+
"Type": "Patch",
6+
"ChangelogMessages": [
7+
"Fixed an issue causing 'lambda package' to hang when ran in CI/CD. The fix applies for Linux-based systems."
8+
]
9+
}
10+
]
11+
}

src/Amazon.Lambda.Tools/LambdaDotNetCLIWrapper.cs

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -184,42 +184,71 @@ public int Publish(LambdaToolsDefaults defaults, string projectLocation, string
184184

185185
// echo the full dotnet command for debug
186186
_logger?.WriteLine($"... dotnet {arguments}");
187-
188-
var psi = new ProcessStartInfo
189-
{
190-
FileName = dotnetCLI,
191-
Arguments = arguments,
192-
WorkingDirectory = this._workingDirectory,
193-
RedirectStandardOutput = true,
194-
RedirectStandardError = true,
195-
UseShellExecute = false,
196-
CreateNoWindow = true
197-
};
198-
187+
int exitCode;
199188
var handler = (DataReceivedEventHandler)((o, e) =>
200189
{
201190
if (string.IsNullOrEmpty(e.Data))
202191
return;
203192
_logger?.WriteLine("... publish: " + e.Data);
204193
});
205-
206-
int exitCode;
207-
using (var proc = new Process())
194+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
208195
{
209-
proc.StartInfo = psi;
210-
proc.Start();
196+
var psi = new ProcessStartInfo
197+
{
198+
FileName = dotnetCLI,
199+
Arguments = arguments,
200+
WorkingDirectory = this._workingDirectory,
201+
RedirectStandardOutput = true,
202+
RedirectStandardError = true,
203+
UseShellExecute = false,
204+
CreateNoWindow = true
205+
};
206+
207+
using (var proc = new Process())
208+
{
209+
proc.StartInfo = psi;
210+
proc.Start();
211211

212212

213-
proc.ErrorDataReceived += handler;
214-
proc.OutputDataReceived += handler;
215-
proc.BeginOutputReadLine();
216-
proc.BeginErrorReadLine();
213+
proc.ErrorDataReceived += handler;
214+
proc.OutputDataReceived += handler;
215+
proc.BeginOutputReadLine();
216+
proc.BeginErrorReadLine();
217217

218-
proc.EnableRaisingEvents = true;
218+
proc.EnableRaisingEvents = true;
219219

220-
proc.WaitForExit();
220+
proc.WaitForExit();
221221

222-
exitCode = proc.ExitCode;
222+
exitCode = proc.ExitCode;
223+
}
224+
}
225+
else
226+
{
227+
var tempOutputFile = Path.GetTempFileName();
228+
var psi = new ProcessStartInfo
229+
{
230+
FileName = GetSystemShell(),
231+
Arguments = $"-c \"{dotnetCLI} {arguments} > '{tempOutputFile}' 2>&1\"",
232+
WorkingDirectory = this._workingDirectory,
233+
UseShellExecute = true,
234+
CreateNoWindow = true
235+
};
236+
237+
using (var proc = new Process())
238+
{
239+
proc.StartInfo = psi;
240+
proc.Start();
241+
242+
proc.EnableRaisingEvents = true;
243+
244+
proc.WaitForExit();
245+
246+
var output = File.ReadAllText(tempOutputFile);
247+
_logger?.WriteLine(output);
248+
File.Delete(tempOutputFile);
249+
250+
exitCode = proc.ExitCode;
251+
}
223252
}
224253

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

271300
return exitCode;
272301
}
302+
303+
private string GetSystemShell()
304+
{
305+
if (TryGetEnvironmentVariable("COMSPEC", out var comspec))
306+
return comspec!;
307+
308+
if (TryGetEnvironmentVariable("SHELL", out var shell))
309+
return shell!;
310+
311+
// fall back to defaults
312+
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "/bin/sh";
313+
}
314+
315+
private bool TryGetEnvironmentVariable(string variable, out string value)
316+
{
317+
value = Environment.GetEnvironmentVariable(variable);
318+
319+
return !string.IsNullOrEmpty(value);
320+
}
273321

274322
public string GetPublishArguments(string projectLocation,
275323
string outputLocation,

0 commit comments

Comments
 (0)