From c78689a6e18ad5ea224e5a5a19b0fe933472478a Mon Sep 17 00:00:00 2001 From: Thomas Jespersen Date: Mon, 27 May 2024 18:59:26 +0200 Subject: [PATCH] Show error message if GitHub workflow fails and avoid starting the next GitHub workflow --- .../ConfigureContinuousDeploymentsCommand.cs | 62 ++++++++++++------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/developer-cli/Commands/ConfigureContinuousDeploymentsCommand.cs b/developer-cli/Commands/ConfigureContinuousDeploymentsCommand.cs index c6fae3354..e3db7cfa7 100644 --- a/developer-cli/Commands/ConfigureContinuousDeploymentsCommand.cs +++ b/developer-cli/Commands/ConfigureContinuousDeploymentsCommand.cs @@ -755,41 +755,57 @@ private void TriggerAndMonitorWorkflows() void StartGithubWorkflow(string workflowName, string workflowFileName) { - AnsiConsole.MarkupLine($"[green]Starting {workflowName} GitHub workflow...[/]"); + try + { + AnsiConsole.MarkupLine($"[green]Starting {workflowName} GitHub workflow...[/]"); - var runWorkflowCommand = $"gh workflow run {workflowFileName} --ref main"; - ProcessHelper.StartProcess(runWorkflowCommand, Configuration.GetSourceCodeFolder(), true); + var runWorkflowCommand = $"gh workflow run {workflowFileName} --ref main"; + ProcessHelper.StartProcess(runWorkflowCommand, Configuration.GetSourceCodeFolder(), true); - // Wait briefly to ensure the run has started - Thread.Sleep(TimeSpan.FromSeconds(15)); + // Wait briefly to ensure the run has started + Thread.Sleep(TimeSpan.FromSeconds(15)); - // Fetch and filter the workflows to find a "running" one - var listWorkflowRunsCommand = $"gh run list --workflow={workflowFileName} --json databaseId,status"; - var workflowsJson = ProcessHelper.StartProcess(listWorkflowRunsCommand, Configuration.GetSourceCodeFolder(), true); + // Fetch and filter the workflows to find a "running" one + var listWorkflowRunsCommand = $"gh run list --workflow={workflowFileName} --json databaseId,status"; + var workflowsJson = ProcessHelper.StartProcess(listWorkflowRunsCommand, Configuration.GetSourceCodeFolder(), true); - long? workflowId = null; - using (var jsonDocument = JsonDocument.Parse(workflowsJson)) - { - foreach (var element in jsonDocument.RootElement.EnumerateArray()) + long? workflowId = null; + using (var jsonDocument = JsonDocument.Parse(workflowsJson)) { - var status = element.GetProperty("status").GetString()!; - workflowId = element.GetProperty("databaseId").GetInt64(); - - if (status.Equals("in_progress", StringComparison.OrdinalIgnoreCase)) + foreach (var element in jsonDocument.RootElement.EnumerateArray()) { - break; + var status = element.GetProperty("status").GetString()!; + + if (status.Equals("in_progress", StringComparison.OrdinalIgnoreCase)) + { + workflowId = element.GetProperty("databaseId").GetInt64(); + break; + } } } - } - if (workflowId is null) + if (workflowId is null) + { + AnsiConsole.MarkupLine("[red]Failed to retrieve a running workflow ID. Please check the GitHub Actions page for more info.[/]"); + Environment.Exit(1); + } + + var watchWorkflowRunCommand = $"gh run watch {workflowId.Value}"; + ProcessHelper.StartProcessWithSystemShell(watchWorkflowRunCommand, Configuration.GetSourceCodeFolder()); + + // Run the command one more time to get the result + var runResult = ProcessHelper.StartProcess(watchWorkflowRunCommand, Configuration.GetSourceCodeFolder(), true); + if (runResult.Contains("completed") && runResult.Contains("success")) return; + + AnsiConsole.MarkupLine($"[red]Error: Failed to run the {workflowName} GitHub workflow.[/]"); + AnsiConsole.MarkupLine($"[red]{runResult}[/]"); + Environment.Exit(1); + } + catch (Exception) { - AnsiConsole.MarkupLine("[red]Failed to retrieve a running workflow ID.[/]"); + AnsiConsole.MarkupLine($"[red]Error: Failed to run the {workflowName} GitHub workflow.[/]"); Environment.Exit(1); } - - var watchWorkflowRunCommand = $"gh run watch {workflowId.Value} --exit-status"; - ProcessHelper.StartProcessWithSystemShell(watchWorkflowRunCommand, Configuration.GetSourceCodeFolder()); } }