Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/Runner.Worker/JobContext.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using GitHub.DistributedTask.Pipelines.ContextData;
using GitHub.DistributedTask.Pipelines.ContextData;
using GitHub.Runner.Common.Util;
using GitHub.Runner.Common;
using System;
using System.Collections.Generic;

namespace GitHub.Runner.Worker
{
public sealed class JobContext : DictionaryContextData
public sealed class JobContext : DictionaryContextData, IEnvironmentContextData
{
public ActionResult? Status
{
Expand Down Expand Up @@ -146,5 +148,33 @@ public string WorkflowFilePath
this["workflow_file_path"] = value != null ? new StringContextData(value) : null;
}
}

private readonly HashSet<string> _contextEnvAllowlist = new(StringComparer.OrdinalIgnoreCase)
{
"check_run_id",
"status",
"workflow_ref",
"workflow_sha",
"workflow_repository",
"workflow_file_path",
};

public IEnumerable<KeyValuePair<string, string>> GetRuntimeEnvironmentVariables()
{
foreach (var data in this)
{
if (_contextEnvAllowlist.Contains(data.Key))
{
if (data.Value is StringContextData value)
{
yield return new KeyValuePair<string, string>($"JOB_{data.Key.ToUpperInvariant()}", value.ToString());
}
else if (data.Value is NumberContextData numberValue)
{
yield return new KeyValuePair<string, string>($"JOB_{data.Key.ToUpperInvariant()}", numberValue.ToString());
}
}
}
}
}
}
27 changes: 26 additions & 1 deletion src/Test/L0/Worker/JobContextL0.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System;
using System.Collections.Generic;
using GitHub.DistributedTask.Pipelines.ContextData;
using GitHub.Runner.Worker;
using Xunit;
Expand Down Expand Up @@ -138,5 +139,29 @@ public void WorkflowFilePath_SetNull_ClearsValue()
ctx.WorkflowFilePath = null;
Assert.Null(ctx.WorkflowFilePath);
}

[Fact]
public void GetRuntimeEnvironmentVariables_ReturnsCorrectVariables()
{
var ctx = new JobContext();
ctx.CheckRunId = 12345;
ctx.Status = ActionResult.Success;
ctx.WorkflowRef = "owner/repo/.github/workflows/ci.yml@refs/heads/main";
ctx.WorkflowSha = "abc123def456";
ctx.WorkflowRepository = "owner/repo";
ctx.WorkflowFilePath = ".github/workflows/ci.yml";

var dict = new Dictionary<string, string>(ctx.GetRuntimeEnvironmentVariables());
Assert.Equal("12345", dict["JOB_CHECK_RUN_ID"]);
Assert.Equal("success", dict["JOB_STATUS"]);
Assert.Equal("owner/repo/.github/workflows/ci.yml@refs/heads/main", dict["JOB_WORKFLOW_REF"]);
Assert.Equal("abc123def456", dict["JOB_WORKFLOW_SHA"]);
Assert.Equal("owner/repo", dict["JOB_WORKFLOW_REPOSITORY"]);
Assert.Equal(".github/workflows/ci.yml", dict["JOB_WORKFLOW_FILE_PATH"]);

ctx = new JobContext();
dict = new Dictionary<string, string>(ctx.GetRuntimeEnvironmentVariables());
Assert.Empty(dict);
}
}
}