Skip to content

Commit

Permalink
Show error message if GitHub workflow fails and avoid starting the ne…
Browse files Browse the repository at this point in the history
…xt GitHub workflow
  • Loading branch information
tjementum committed May 27, 2024
1 parent d156527 commit c78689a
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions developer-cli/Commands/ConfigureContinuousDeploymentsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down

0 comments on commit c78689a

Please sign in to comment.