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
28 changes: 27 additions & 1 deletion 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.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 @@ -82,5 +84,29 @@ public double? CheckRunId
}
}
}

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

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);
Copy link

Copilot AI Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implicit conversion from StringContextData to string may not work as expected. Use value.ToString() to explicitly convert the value to a string.

Suggested change
yield return new KeyValuePair<string, string>($"JOB_{data.Key.ToUpperInvariant()}", value);
yield return new KeyValuePair<string, string>($"JOB_{data.Key.ToUpperInvariant()}", value.ToString());

Copilot uses AI. Check for mistakes.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would go for a GITHUB_JOB_ prefix instead of just JOB_ to make it more consistent to other environment variables and to point out that these variables are github context variables.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not a bad idea, I'm curious to see what the maintainers think. I had originally considered that, but:

  • It makes it less consistent with how other context -> env vars work, i.e. github. -> GITHUB_ and runner. -> RUNNER_
  • There's potentially a risk of collisions if any github.job_... fields are added in the future, since those would also have a GITHUB_JOB_ prefix

Personally my preference would have been to name the context field github.job_id, in which case the env var could just be GITHUB_JOB_ID, but I think that ship has sailed 🫤

cc @ericsciple

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jenseng I think you are right your name pattern makes sense then

}
else if (data.Value is NumberContextData numberValue)
{
yield return new KeyValuePair<string, string>($"JOB_{data.Key.ToUpperInvariant()}", numberValue.ToString());
}
}
}
}
}
}
17 changes: 17 additions & 0 deletions src/Test/L0/Worker/JobContextL0.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using GitHub.DistributedTask.Pipelines.ContextData;
using GitHub.Runner.Worker;
using Xunit;
Expand Down Expand Up @@ -34,5 +35,21 @@ public void CheckRunId_SetNull_RemovesKey()
ctx.CheckRunId = null;
Assert.Null(ctx.CheckRunId);
}

[Fact]
public void GetRuntimeEnvironmentVariables_ReturnsCorrectVariables()
{
var ctx = new JobContext();
ctx.CheckRunId = 12345;
ctx.Status = ActionResult.Success;

var dict = new Dictionary<string, string>(ctx.GetRuntimeEnvironmentVariables());
Assert.Equal("12345", dict["JOB_CHECK_RUN_ID"]);
Assert.Equal("success", dict["JOB_STATUS"]);

ctx.CheckRunId = null;
dict = new Dictionary<string, string>(ctx.GetRuntimeEnvironmentVariables());
Assert.False(dict.ContainsKey("JOB_CHECK_RUN_ID"));
}
}
}