Skip to content

Commit 81ace3c

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

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
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"
8+
]
9+
}
10+
]
11+
}

src/Amazon.Lambda.Tools/LambdaDotNetCLIWrapper.cs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -184,41 +184,39 @@ public int Publish(LambdaToolsDefaults defaults, string projectLocation, string
184184

185185
// echo the full dotnet command for debug
186186
_logger?.WriteLine($"... dotnet {arguments}");
187-
187+
var tempOutputFile = Path.GetTempFileName();
188188
var psi = new ProcessStartInfo
189189
{
190-
FileName = dotnetCLI,
191-
Arguments = arguments,
190+
FileName = GetSystemShell(),
191+
Arguments = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
192+
$"/c {dotnetCLI} {arguments} > \"{tempOutputFile}\" 2>&1" :
193+
$"-c \"{dotnetCLI} {arguments} > '{tempOutputFile}' 2>&1\"",
192194
WorkingDirectory = this._workingDirectory,
193-
RedirectStandardOutput = true,
194-
RedirectStandardError = true,
195-
UseShellExecute = false,
195+
UseShellExecute = true,
196196
CreateNoWindow = true
197197
};
198-
198+
199199
var handler = (DataReceivedEventHandler)((o, e) =>
200200
{
201201
if (string.IsNullOrEmpty(e.Data))
202202
return;
203203
_logger?.WriteLine("... publish: " + e.Data);
204204
});
205-
205+
206206
int exitCode;
207207
using (var proc = new Process())
208208
{
209209
proc.StartInfo = psi;
210210
proc.Start();
211-
212-
213-
proc.ErrorDataReceived += handler;
214-
proc.OutputDataReceived += handler;
215-
proc.BeginOutputReadLine();
216-
proc.BeginErrorReadLine();
217-
211+
218212
proc.EnableRaisingEvents = true;
219-
213+
220214
proc.WaitForExit();
221-
215+
216+
var output = File.ReadAllText(tempOutputFile);
217+
_logger?.WriteLine(output);
218+
File.Delete(tempOutputFile);
219+
222220
exitCode = proc.ExitCode;
223221
}
224222

@@ -270,6 +268,25 @@ public int Publish(LambdaToolsDefaults defaults, string projectLocation, string
270268

271269
return exitCode;
272270
}
271+
272+
private string GetSystemShell()
273+
{
274+
if (TryGetEnvironmentVariable("COMSPEC", out var comspec))
275+
return comspec!;
276+
277+
if (TryGetEnvironmentVariable("SHELL", out var shell))
278+
return shell!;
279+
280+
// fall back to defaults
281+
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "/bin/sh";
282+
}
283+
284+
private bool TryGetEnvironmentVariable(string variable, out string value)
285+
{
286+
value = Environment.GetEnvironmentVariable(variable);
287+
288+
return !string.IsNullOrEmpty(value);
289+
}
273290

274291
public string GetPublishArguments(string projectLocation,
275292
string outputLocation,

0 commit comments

Comments
 (0)