Skip to content

Commit

Permalink
gh cli auth (#534)
Browse files Browse the repository at this point in the history
* call the gh auth token for non azure pipelines workloads
* prepend this github_token
* fix setting values to empty string, e.g. `-s github_token=` to unset
  • Loading branch information
ChristopherHX authored Jan 31, 2025
1 parent f20a07c commit 59c0337
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/Runner.Client/GhCliAuth.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using GitHub.Runner.Sdk;

namespace Runner.Client
{
public class GhCliAuth {
public static async Task<string> GetToken(string workingDirectory, ITraceWriter trace, CancellationToken token) {
string line = null;
void handleOutput(object s, ProcessDataReceivedEventArgs e)
{
line ??= e.Data;
}
try {
var gh = WhichUtil.Which("gh", require: false, trace: trace);
if (string.IsNullOrWhiteSpace(gh))
{
return null;
}
var ghInvoker = new ProcessInvoker(trace);
ghInvoker.OutputDataReceived += handleOutput;
await ghInvoker.ExecuteAsync(workingDirectory, gh, "auth token", new Dictionary<string, string>(), token);
return line;
} catch {
return null;
}
}
}
}
6 changes: 6 additions & 0 deletions src/Runner.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,12 @@ await Task.WhenAny(Task.Run(() => {
}
}
}
if(parameters.Event != "azpipelines") {
var githubToken = await GhCliAuth.GetToken(null, new TraceWriter(parameters), CancellationToken.None);
if(githubToken != null) {
wsecrets.Add($"github_token={githubToken}");
}
}
if(parameters.Secrets?.Length > 0) {
for(int i = 0; i < parameters.Secrets.Length; i++ ) {
var e = parameters.Secrets[i];
Expand Down
2 changes: 1 addition & 1 deletion src/Runner.Server/Controllers/MessageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ private static void LoadEnvSec(string[] contents, Action<string, string> kvhandl
if (separatorIndex > 0)
{
string envKey = env.Substring(0, separatorIndex);
string envValue = null;
string envValue = "";
if (env.Length > separatorIndex + 1)
{
envValue = env.Substring(separatorIndex + 1);
Expand Down

0 comments on commit 59c0337

Please sign in to comment.