From 22a9d8977205395ed939855295151a5d373c8820 Mon Sep 17 00:00:00 2001 From: Thomas Boop <52323235+thboop@users.noreply.github.com> Date: Wed, 4 Aug 2021 11:39:22 -0400 Subject: [PATCH] Correctly set post step step context (#1243) --- src/Runner.Worker/ExecutionContext.cs | 24 ++++++++++++------- .../Handlers/CompositeActionHandler.cs | 22 +++++++++++++---- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/Runner.Worker/ExecutionContext.cs b/src/Runner.Worker/ExecutionContext.cs index 61ee9d2c7..6df943f5d 100644 --- a/src/Runner.Worker/ExecutionContext.cs +++ b/src/Runner.Worker/ExecutionContext.cs @@ -38,6 +38,7 @@ namespace GitHub.Runner.Worker Guid Id { get; } Guid EmbeddedId { get; } string ScopeName { get; } + string SiblingScopeName { get; } string ContextName { get; } Task ForceCompleted { get; } TaskResult? Result { get; set; } @@ -74,8 +75,8 @@ namespace GitHub.Runner.Worker // Initialize void InitializeJob(Pipelines.AgentJobRequestMessage message, CancellationToken token); void CancelToken(); - IExecutionContext CreateChild(Guid recordId, string displayName, string refName, string scopeName, string contextName, Dictionary intraActionState = null, int? recordOrder = null, IPagingLogger logger = null, bool isEmbedded = false, CancellationTokenSource cancellationTokenSource = null, Guid embeddedId = default(Guid)); - IExecutionContext CreateEmbeddedChild(string scopeName, string contextName, Guid embeddedId, Dictionary intraActionState = null); + IExecutionContext CreateChild(Guid recordId, string displayName, string refName, string scopeName, string contextName, Dictionary intraActionState = null, int? recordOrder = null, IPagingLogger logger = null, bool isEmbedded = false, CancellationTokenSource cancellationTokenSource = null, Guid embeddedId = default(Guid), string siblingScopeName = null); + IExecutionContext CreateEmbeddedChild(string scopeName, string contextName, Guid embeddedId, Dictionary intraActionState = null, string siblingScopeName = null); // logging long Write(string tag, string message); @@ -140,6 +141,7 @@ namespace GitHub.Runner.Worker public Guid Id => _record.Id; public Guid EmbeddedId { get; private set; } public string ScopeName { get; private set; } + public string SiblingScopeName { get; private set; } public string ContextName { get; private set; } public Task ForceCompleted => _forceCompleted.Task; public CancellationToken CancellationToken => _cancellationTokenSource.Token; @@ -258,6 +260,7 @@ namespace GitHub.Runner.Worker public void RegisterPostJobStep(IStep step) { + string siblingScopeName = null; if (this.IsEmbedded) { if (step is IActionRunner actionRunner && !Root.EmbeddedStepsWithPostRegistered.Add(actionRunner.Action.Id)) @@ -271,12 +274,16 @@ namespace GitHub.Runner.Worker Trace.Info($"'post' of '{actionRunner.DisplayName}' already push to post step stack."); return; } + if (step is IActionRunner runner) + { + siblingScopeName = runner.Action.ContextName; + } - step.ExecutionContext = Root.CreatePostChild(step.DisplayName, IntraActionState); + step.ExecutionContext = Root.CreatePostChild(step.DisplayName, IntraActionState, siblingScopeName); Root.PostJobSteps.Push(step); } - public IExecutionContext CreateChild(Guid recordId, string displayName, string refName, string scopeName, string contextName, Dictionary intraActionState = null, int? recordOrder = null, IPagingLogger logger = null, bool isEmbedded = false, CancellationTokenSource cancellationTokenSource = null, Guid embeddedId = default(Guid)) + public IExecutionContext CreateChild(Guid recordId, string displayName, string refName, string scopeName, string contextName, Dictionary intraActionState = null, int? recordOrder = null, IPagingLogger logger = null, bool isEmbedded = false, CancellationTokenSource cancellationTokenSource = null, Guid embeddedId = default(Guid), string siblingScopeName = null) { Trace.Entering(); @@ -286,6 +293,7 @@ namespace GitHub.Runner.Worker child.ScopeName = scopeName; child.ContextName = contextName; child.EmbeddedId = embeddedId; + child.SiblingScopeName = siblingScopeName; if (intraActionState == null) { child.IntraActionState = new Dictionary(StringComparer.OrdinalIgnoreCase); @@ -333,9 +341,9 @@ namespace GitHub.Runner.Worker /// An embedded execution context shares the same record ID, record name, logger, /// and a linked cancellation token. /// - public IExecutionContext CreateEmbeddedChild(string scopeName, string contextName, Guid embeddedId, Dictionary intraActionState = null) + public IExecutionContext CreateEmbeddedChild(string scopeName, string contextName, Guid embeddedId, Dictionary intraActionState = null, string siblingScopeName = null) { - return Root.CreateChild(_record.Id, _record.Name, _record.Id.ToString("N"), scopeName, contextName, logger: _logger, isEmbedded: true, cancellationTokenSource: CancellationTokenSource.CreateLinkedTokenSource(_cancellationTokenSource.Token), intraActionState: intraActionState, embeddedId: embeddedId); + return Root.CreateChild(_record.Id, _record.Name, _record.Id.ToString("N"), scopeName, contextName, logger: _logger, isEmbedded: true, cancellationTokenSource: CancellationTokenSource.CreateLinkedTokenSource(_cancellationTokenSource.Token), intraActionState: intraActionState, embeddedId: embeddedId, siblingScopeName: siblingScopeName); } public void Start(string currentOperation = null) @@ -914,7 +922,7 @@ namespace GitHub.Runner.Worker } } - private IExecutionContext CreatePostChild(string displayName, Dictionary intraActionState) + private IExecutionContext CreatePostChild(string displayName, Dictionary intraActionState, string siblingScopeName = null) { if (!_expandedForPostJob) { @@ -924,7 +932,7 @@ namespace GitHub.Runner.Worker } var newGuid = Guid.NewGuid(); - return CreateChild(newGuid, displayName, newGuid.ToString("N"), null, null, intraActionState, _childTimelineRecordOrder - Root.PostJobSteps.Count); + return CreateChild(newGuid, displayName, newGuid.ToString("N"), null, null, intraActionState, _childTimelineRecordOrder - Root.PostJobSteps.Count, siblingScopeName: siblingScopeName); } } diff --git a/src/Runner.Worker/Handlers/CompositeActionHandler.cs b/src/Runner.Worker/Handlers/CompositeActionHandler.cs index 3fe8f33bc..89cc43d4e 100644 --- a/src/Runner.Worker/Handlers/CompositeActionHandler.cs +++ b/src/Runner.Worker/Handlers/CompositeActionHandler.cs @@ -120,7 +120,7 @@ namespace GitHub.Runner.Worker.Handlers // only relevant for local composite actions that need to JIT download/setup containers if (LocalActionContainerSetupSteps != null && LocalActionContainerSetupSteps.Count > 0) { - foreach(var step in LocalActionContainerSetupSteps) + foreach (var step in LocalActionContainerSetupSteps) { ArgUtil.NotNull(step, step.DisplayName); var stepId = $"__{Guid.NewGuid()}"; @@ -128,17 +128,31 @@ namespace GitHub.Runner.Worker.Handlers embeddedSteps.Add(step); } } - foreach (Pipelines.ActionStep stepData in steps) { + // Compute child sibling scope names for post steps + // We need to use the main's scope to keep step context correct, makes inputs flow correctly + string siblingScopeName = null; + if (!String.IsNullOrEmpty(ExecutionContext.SiblingScopeName) && stage == ActionRunStage.Post) + { + siblingScopeName = $"{ExecutionContext.SiblingScopeName}.{stepData.ContextName}"; + } + var step = HostContext.CreateService(); step.Action = stepData; step.Stage = stage; step.Condition = stepData.Condition; ExecutionContext.Root.EmbeddedIntraActionState.TryGetValue(step.Action.Id, out var intraActionState); - step.ExecutionContext = ExecutionContext.CreateEmbeddedChild(childScopeName, stepData.ContextName, step.Action.Id, intraActionState: intraActionState); + step.ExecutionContext = ExecutionContext.CreateEmbeddedChild(childScopeName, stepData.ContextName, step.Action.Id, intraActionState: intraActionState, siblingScopeName: siblingScopeName); step.ExecutionContext.ExpressionValues["inputs"] = inputsData; - step.ExecutionContext.ExpressionValues["steps"] = ExecutionContext.Global.StepsContext.GetScope(childScopeName); + if (!String.IsNullOrEmpty(ExecutionContext.SiblingScopeName)) + { + step.ExecutionContext.ExpressionValues["steps"] = ExecutionContext.Global.StepsContext.GetScope(ExecutionContext.SiblingScopeName); + } + else + { + step.ExecutionContext.ExpressionValues["steps"] = ExecutionContext.Global.StepsContext.GetScope(childScopeName); + } // Shallow copy github context var gitHubContext = step.ExecutionContext.ExpressionValues["github"] as GitHubContext;