Files
runner/src/Runner.Worker/GitHubContext.cs
Amit Rathi 98c857b927 expose github.actor_id, github.workflow_ref & github.workflow_sha as environment variable (#2249)
* expose workflow refs/sha  as environment variables

* fixes environment variable ordering

* job_workflow_ref/sha aren't available in gh ctx
2022-11-17 11:11:52 -05:00

81 lines
2.2 KiB
C#

using GitHub.DistributedTask.Pipelines.ContextData;
using System;
using System.Collections.Generic;
namespace GitHub.Runner.Worker
{
public sealed class GitHubContext : DictionaryContextData, IEnvironmentContextData
{
private readonly HashSet<string> _contextEnvAllowlist = new(StringComparer.OrdinalIgnoreCase)
{
"action_path",
"action_ref",
"action_repository",
"action",
"actor",
"actor_id",
"api_url",
"base_ref",
"env",
"event_name",
"event_path",
"graphql_url",
"head_ref",
"job",
"output",
"path",
"ref_name",
"ref_protected",
"ref_type",
"ref",
"repository",
"repository_id",
"repository_owner",
"repository_owner_id",
"retention_days",
"run_attempt",
"run_id",
"run_number",
"server_url",
"sha",
"state",
"step_summary",
"triggering_actor",
"workflow",
"workflow_ref",
"workflow_sha",
"workspace"
};
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>($"GITHUB_{data.Key.ToUpperInvariant()}", value);
}
else if (data.Value is BooleanContextData booleanValue)
{
yield return new KeyValuePair<string, string>($"GITHUB_{data.Key.ToUpperInvariant()}", booleanValue.ToString());
}
}
}
}
public GitHubContext ShallowCopy()
{
var copy = new GitHubContext();
foreach (var pair in this)
{
copy[pair.Key] = pair.Value;
}
return copy;
}
}
}