From dca4f67143ae82672a8ee753b4fd6da347def206 Mon Sep 17 00:00:00 2001 From: Tauhid Anjum Date: Fri, 30 Sep 2022 19:07:47 +0530 Subject: [PATCH] Adding a new vars context for non-secret variables (#2096) * Adding a new vars context for non-secret variables * Fix test case * Trigger checks * Remove variables from env context and environment varibale * remove extra references * Add prefix handling to configuration variables * Fix test cases * Consume variables using vars in context data * removed action_yaml changes --- .../PipelineTemplateConstants.cs | 1 + .../PipelineTemplateConverter.cs | 2 + .../PipelineTemplateEvaluator.cs | 1 + src/Sdk/DTPipelines/workflow-v1.0.json | 51 ++++++++++++++----- src/Test/L0/Worker/ExecutionContextL0.cs | 45 ++++++++++++++++ 5 files changed, 86 insertions(+), 14 deletions(-) diff --git a/src/Sdk/DTPipelines/Pipelines/ObjectTemplating/PipelineTemplateConstants.cs b/src/Sdk/DTPipelines/Pipelines/ObjectTemplating/PipelineTemplateConstants.cs index 7d7bf14e7..e2e635622 100644 --- a/src/Sdk/DTPipelines/Pipelines/ObjectTemplating/PipelineTemplateConstants.cs +++ b/src/Sdk/DTPipelines/Pipelines/ObjectTemplating/PipelineTemplateConstants.cs @@ -73,6 +73,7 @@ namespace GitHub.DistributedTask.Pipelines.ObjectTemplating public const String TimeoutMinutes = "timeout-minutes"; public const String Username = "username"; public const String Uses = "uses"; + public const String Vars = "vars"; public const String VmImage = "vmImage"; public const String Volumes = "volumes"; public const String With = "with"; diff --git a/src/Sdk/DTPipelines/Pipelines/ObjectTemplating/PipelineTemplateConverter.cs b/src/Sdk/DTPipelines/Pipelines/ObjectTemplating/PipelineTemplateConverter.cs index 2cdde1a24..69f9a3d32 100644 --- a/src/Sdk/DTPipelines/Pipelines/ObjectTemplating/PipelineTemplateConverter.cs +++ b/src/Sdk/DTPipelines/Pipelines/ObjectTemplating/PipelineTemplateConverter.cs @@ -631,6 +631,7 @@ namespace GitHub.DistributedTask.Pipelines.ObjectTemplating { new NamedValueInfo(PipelineTemplateConstants.GitHub), new NamedValueInfo(PipelineTemplateConstants.Needs), + new NamedValueInfo(PipelineTemplateConstants.Vars), }; private static readonly INamedValueInfo[] s_stepNamedValues = new INamedValueInfo[] { @@ -643,6 +644,7 @@ namespace GitHub.DistributedTask.Pipelines.ObjectTemplating new NamedValueInfo(PipelineTemplateConstants.Runner), new NamedValueInfo(PipelineTemplateConstants.Env), new NamedValueInfo(PipelineTemplateConstants.Needs), + new NamedValueInfo(PipelineTemplateConstants.Vars), }; private static readonly IFunctionInfo[] s_stepConditionFunctions = new IFunctionInfo[] { diff --git a/src/Sdk/DTPipelines/Pipelines/ObjectTemplating/PipelineTemplateEvaluator.cs b/src/Sdk/DTPipelines/Pipelines/ObjectTemplating/PipelineTemplateEvaluator.cs index 74532b101..43d9d4faa 100644 --- a/src/Sdk/DTPipelines/Pipelines/ObjectTemplating/PipelineTemplateEvaluator.cs +++ b/src/Sdk/DTPipelines/Pipelines/ObjectTemplating/PipelineTemplateEvaluator.cs @@ -465,6 +465,7 @@ namespace GitHub.DistributedTask.Pipelines.ObjectTemplating PipelineTemplateConstants.Job, PipelineTemplateConstants.Runner, PipelineTemplateConstants.Env, + PipelineTemplateConstants.Vars, }; private readonly String[] s_expressionFunctionNames = new[] { diff --git a/src/Sdk/DTPipelines/workflow-v1.0.json b/src/Sdk/DTPipelines/workflow-v1.0.json index 1ebc81065..6ae301fbd 100644 --- a/src/Sdk/DTPipelines/workflow-v1.0.json +++ b/src/Sdk/DTPipelines/workflow-v1.0.json @@ -36,7 +36,8 @@ "workflow-env": { "context": [ "github", - "secrets" + "secrets", + "vars" ], "mapping": { "loose-key-type": "non-empty-string", @@ -86,6 +87,7 @@ "context": [ "github", "needs", + "vars", "always(0,0)", "failure(0,MAX)", "cancelled(0,0)", @@ -98,6 +100,7 @@ "context": [ "github", "needs", + "vars", "always(0,0)", "failure(0,MAX)", "cancelled(0,0)", @@ -116,7 +119,8 @@ "strategy": { "context": [ "github", - "needs" + "needs", + "vars" ], "mapping": { "properties": { @@ -156,7 +160,8 @@ "github", "needs", "strategy", - "matrix" + "matrix", + "vars" ], "one-of": [ "non-empty-string", @@ -182,7 +187,8 @@ "needs", "strategy", "matrix", - "secrets" + "secrets", + "vars" ], "mapping": { "loose-key-type": "non-empty-string", @@ -204,7 +210,8 @@ "strategy", "matrix", "needs", - "env" + "env", + "vars" ], "mapping": { "properties": { @@ -281,6 +288,7 @@ "job", "runner", "env", + "vars", "always(0,0)", "failure(0,0)", "cancelled(0,0)", @@ -299,6 +307,7 @@ "job", "runner", "env", + "vars", "always(0,0)", "failure(0,0)", "cancelled(0,0)", @@ -326,6 +335,7 @@ "job", "runner", "env", + "vars", "hashFiles(1,255)" ], "mapping": { @@ -345,6 +355,7 @@ "job", "runner", "env", + "vars", "hashFiles(1,255)" ], "mapping": { @@ -358,7 +369,8 @@ "github", "needs", "strategy", - "matrix" + "matrix", + "vars" ], "one-of": [ "string", @@ -384,7 +396,8 @@ "github", "needs", "strategy", - "matrix" + "matrix", + "vars" ], "mapping": { "loose-key-type": "non-empty-string", @@ -397,7 +410,8 @@ "github", "needs", "strategy", - "matrix" + "matrix", + "vars" ], "one-of": [ "non-empty-string", @@ -409,7 +423,8 @@ "context": [ "secrets", "env", - "github" + "github", + "vars" ], "mapping": { "properties": { @@ -443,7 +458,8 @@ "github", "needs", "strategy", - "matrix" + "matrix", + "vars" ], "boolean": {} }, @@ -453,7 +469,8 @@ "github", "needs", "strategy", - "matrix" + "matrix", + "vars" ], "number": {} }, @@ -463,7 +480,8 @@ "github", "needs", "strategy", - "matrix" + "matrix", + "vars" ], "string": {} }, @@ -479,6 +497,7 @@ "job", "runner", "env", + "vars", "hashFiles(1,255)" ], "boolean": {} @@ -495,6 +514,7 @@ "job", "runner", "env", + "vars", "hashFiles(1,255)" ], "number": {} @@ -510,7 +530,8 @@ "steps", "job", "runner", - "env" + "env", + "vars" ], "string": {} }, @@ -524,7 +545,8 @@ "steps", "job", "runner", - "env" + "env", + "vars" ], "string": {} }, @@ -540,6 +562,7 @@ "job", "runner", "env", + "vars", "hashFiles(1,255)" ], "string": {} diff --git a/src/Test/L0/Worker/ExecutionContextL0.cs b/src/Test/L0/Worker/ExecutionContextL0.cs index 6ee0161a7..670fece20 100644 --- a/src/Test/L0/Worker/ExecutionContextL0.cs +++ b/src/Test/L0/Worker/ExecutionContextL0.cs @@ -844,6 +844,51 @@ namespace GitHub.Runner.Common.Tests.Worker } } + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Worker")] + public void ActionVariables_AddedToVarsContext() + { + using (TestHostContext hc = CreateTestContext()) + { + TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); + TimelineReference timeline = new TimelineReference(); + Guid jobId = Guid.NewGuid(); + string jobName = "some job name"; + var jobRequest = new Pipelines.AgentJobRequestMessage(plan, timeline, jobId, jobName, jobName, null, null, null, new Dictionary(), new List(), new Pipelines.JobResources(), new Pipelines.ContextData.DictionaryContextData(), new Pipelines.WorkspaceOptions(), new List(), null, null, null, null); + jobRequest.Resources.Repositories.Add(new Pipelines.RepositoryResource() + { + Alias = Pipelines.PipelineConstants.SelfAlias, + Id = "github", + Version = "sha1" + }); + jobRequest.ContextData["github"] = new Pipelines.ContextData.DictionaryContextData(); + + var inputVarsContext = new DictionaryContextData(); + + inputVarsContext["VARIABLE_1"] = new StringContextData("value1"); + inputVarsContext["VARIABLE_2"] = new StringContextData("value2"); + jobRequest.ContextData["vars"] = inputVarsContext; + + // Arrange: Setup the paging logger. + var pagingLogger1 = new Mock(); + var jobServerQueue = new Mock(); + hc.EnqueueInstance(pagingLogger1.Object); + hc.SetSingleton(jobServerQueue.Object); + + var jobContext = new Runner.Worker.ExecutionContext(); + jobContext.Initialize(hc); + + jobContext.InitializeJob(jobRequest, CancellationToken.None); + + var expected = new DictionaryContextData(); + expected["VARIABLE_1"] = new StringContextData("value1"); + expected["VARIABLE_2"] = new StringContextData("value1"); + + Assert.True(ExpressionValuesAssertEqual(expected, jobContext.ExpressionValues["vars"] as DictionaryContextData)); + } + } + private bool ExpressionValuesAssertEqual(DictionaryContextData expect, DictionaryContextData actual) { foreach (var key in expect.Keys.ToList())