Try adding steps in JobExtension

This commit is contained in:
Brian Cristante
2021-10-14 16:26:00 -04:00
parent 07cda747b2
commit ec420bab24
4 changed files with 127 additions and 58 deletions

View File

@@ -488,44 +488,48 @@ namespace GitHub.Runner.Worker
else if (action.Reference.Type == Pipelines.ActionSourceType.Script) else if (action.Reference.Type == Pipelines.ActionSourceType.Script)
{ {
// Load the inputs. // Load the inputs.
executionContext.Debug("Loading inputs"); // executionContext.Debug("Loading inputs");
var templateEvaluator = executionContext.ToPipelineTemplateEvaluator(); // var templateEvaluator = executionContext.ToPipelineTemplateEvaluator();
var inputs = templateEvaluator.EvaluateStepInputs(action.Inputs, executionContext.ExpressionValues, executionContext.ExpressionFunctions); // var inputs = templateEvaluator.EvaluateStepInputs(action.Inputs, executionContext.ExpressionValues, executionContext.ExpressionFunctions);
// Check if we are running a Makefile. // Check if we are running a Makefile.
// Only works for the default target right now. // Only works for the default target right now.
if (inputs["script"] == "make") // if (inputs["script"] == "make")
{ // {
// Get the path of the Makefile in the repository root. // // Get the path of the Makefile in the repository root.
var githubContext = executionContext.ExpressionValues["github"] as GitHubContext; // var githubContext = executionContext.ExpressionValues["github"] as GitHubContext;
var workspaceDir = githubContext["workspace"] as StringContextData; // var workspaceDir = githubContext["workspace"] as StringContextData;
var makefile = Path.Combine(workspaceDir, "Makefile"); // var makefile = Path.Combine(workspaceDir, "Makefile");
if (!File.Exists(makefile)) // if (!File.Exists(makefile))
{ // {
// Forget about trying to be smart. Just do the normal thing. // // Forget about trying to be smart. Just do the normal thing.
definition.Data.Execution = new ScriptActionExecutionData(); // definition.Data.Execution = new ScriptActionExecutionData();
definition.Data.Name = "Run"; // definition.Data.Name = "Run";
definition.Data.Description = "Execute a script"; // definition.Data.Description = "Execute a script";
} // }
// else
// Assume the default target is named `all`. // {
var definitionData = MakefileManager.Load(executionContext, makefile, target: "all"); // // Assume the default target is named `all`.
if (definitionData is null) // var definitionData = MakefileManager.Load(executionContext, makefile, target: "all");
{ // if (definitionData is null)
// Forget about trying to be smart. Just do the normal thing. // {
definition.Data.Execution = new ScriptActionExecutionData(); // // Forget about trying to be smart. Just do the normal thing.
definition.Data.Name = "Run"; // definition.Data.Execution = new ScriptActionExecutionData();
definition.Data.Description = "Execute a script"; // definition.Data.Name = "Run";
} // definition.Data.Description = "Execute a script";
// }
definition.Data = definitionData; // else
} // {
else // definition.Data = definitionData;
{ // }
// }
// }
// else
// {
definition.Data.Execution = new ScriptActionExecutionData(); definition.Data.Execution = new ScriptActionExecutionData();
definition.Data.Name = "Run"; definition.Data.Name = "Run";
definition.Data.Description = "Execute a script"; definition.Data.Description = "Execute a script";
} // }
} }
else else
{ {

View File

@@ -262,6 +262,16 @@ namespace GitHub.Runner.Worker
environment[$"STATE_{state.Key}"] = state.Value ?? string.Empty; environment[$"STATE_{state.Key}"] = state.Value ?? string.Empty;
} }
// HACK
if (Action.DisplayName != null && Action.DisplayName.StartsWith("make dependency"))
{
var target = Action.DisplayName.Split()[2];
inputs = new Dictionary<string, string>
{
["script"] = $"make {target}"
};
}
// Create the handler. // Create the handler.
IHandler handler = handlerFactory.Create( IHandler handler = handlerFactory.Create(
ExecutionContext, ExecutionContext,

View File

@@ -265,29 +265,78 @@ namespace GitHub.Runner.Worker
if (step.Type == Pipelines.StepType.Action) if (step.Type == Pipelines.StepType.Action)
{ {
var action = step as Pipelines.ActionStep; var action = step as Pipelines.ActionStep;
Trace.Info($"Adding {action.DisplayName}.");
var actionRunner = HostContext.CreateService<IActionRunner>(); void EmitStep()
actionRunner.Action = action;
actionRunner.Stage = ActionRunStage.Main;
actionRunner.Condition = step.Condition;
var contextData = new Pipelines.ContextData.DictionaryContextData();
if (message.ContextData?.Count > 0)
{ {
foreach (var pair in message.ContextData) Trace.Info($"Adding {action.DisplayName}.");
var actionRunner = HostContext.CreateService<IActionRunner>();
actionRunner.Action = action;
actionRunner.Stage = ActionRunStage.Main;
actionRunner.Condition = step.Condition;
var contextData = new Pipelines.ContextData.DictionaryContextData();
if (message.ContextData?.Count > 0)
{ {
contextData[pair.Key] = pair.Value; foreach (var pair in message.ContextData)
{
contextData[pair.Key] = pair.Value;
}
}
actionRunner.TryEvaluateDisplayName(contextData, context);
jobSteps.Add(actionRunner);
if (prepareResult.PreStepTracker.TryGetValue(step.Id, out var preStep))
{
Trace.Info($"Adding pre-{action.DisplayName}.");
preStep.TryEvaluateDisplayName(contextData, context);
preStep.DisplayName = $"Pre {preStep.DisplayName}";
preJobSteps.Add(preStep);
} }
} }
actionRunner.TryEvaluateDisplayName(contextData, context); // HACK: if the step is "run: make," parse the Makefile and emit $"make dependency {target}"
jobSteps.Add(actionRunner); if (action.DisplayName == "Run make")
if (prepareResult.PreStepTracker.TryGetValue(step.Id, out var preStep))
{ {
Trace.Info($"Adding pre-{action.DisplayName}."); // Load the inputs.
preStep.TryEvaluateDisplayName(contextData, context); jobContext.Debug("Loading inputs");
preStep.DisplayName = $"Pre {preStep.DisplayName}"; var inputs = templateEvaluator.EvaluateStepInputs(action.Inputs, jobContext.ExpressionValues, jobContext.ExpressionFunctions);
preJobSteps.Add(preStep);
// Check if we are running a Makefile.
// Only works for the default target right now.
if (inputs["script"] == "make")
{
// Get the path of the Makefile in the repository root.
var githubContext = jobContext.ExpressionValues["github"] as GitHubContext;
var workspaceDir = githubContext["workspace"] as StringContextData;
var makefile = Path.Combine(workspaceDir, "Makefile");
if (!File.Exists(makefile))
{
// Forget about trying to be smart. Just do the normal thing.
EmitStep();
}
else
{
// Assume the default target is named `all`.
var targetDependencies = MakefileManager.ReadTargetDependencies(jobContext, makefile, target: "all");
if (targetDependencies.Count == 0)
{
// Forget about trying to be smart. Just do the normal thing.
EmitStep();
}
else
{
foreach (var target in targetDependencies)
{
action.DisplayName = $"make dependency {target}";
EmitStep();
}
}
}
}
}
else
{
EmitStep();
} }
} }
} }

View File

@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@@ -9,15 +10,7 @@ namespace GitHub.Runner.Worker
// Does not recurse into the dependencies of those steps. // Does not recurse into the dependencies of those steps.
public static ActionDefinitionData Load(IExecutionContext executionContext, string makefile, string target) public static ActionDefinitionData Load(IExecutionContext executionContext, string makefile, string target)
{ {
var targetToFind = target + ":"; var dependencies = ReadTargetDependencies(executionContext, makefile, target);
var lines = File.ReadLines(makefile);
string targetLine = lines.FirstOrDefault(line => line.TrimStart().StartsWith(targetToFind));
if (targetLine is null)
{
return null;
}
var dependencies = targetLine.Split().Skip(1).ToList();
if (dependencies.Count == 0) if (dependencies.Count == 0)
{ {
return null; return null;
@@ -35,5 +28,18 @@ namespace GitHub.Runner.Worker
} }
}; };
} }
public static List<string> ReadTargetDependencies(IExecutionContext executionContext, string makefile, string target)
{
var targetToFind = target + ":";
var lines = File.ReadLines(makefile);
string targetLine = lines.FirstOrDefault(line => line.TrimStart().StartsWith(targetToFind));
if (targetLine is null)
{
return null;
}
return targetLine.Split().Skip(1).ToList();
}
} }
} }