Skip to content

Commit 59c0337

Browse files
gh cli auth (#534)
* 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
1 parent f20a07c commit 59c0337

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

src/Runner.Client/GhCliAuth.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using GitHub.Runner.Sdk;
7+
8+
namespace Runner.Client
9+
{
10+
public class GhCliAuth {
11+
public static async Task<string> GetToken(string workingDirectory, ITraceWriter trace, CancellationToken token) {
12+
string line = null;
13+
void handleOutput(object s, ProcessDataReceivedEventArgs e)
14+
{
15+
line ??= e.Data;
16+
}
17+
try {
18+
var gh = WhichUtil.Which("gh", require: false, trace: trace);
19+
if (string.IsNullOrWhiteSpace(gh))
20+
{
21+
return null;
22+
}
23+
var ghInvoker = new ProcessInvoker(trace);
24+
ghInvoker.OutputDataReceived += handleOutput;
25+
await ghInvoker.ExecuteAsync(workingDirectory, gh, "auth token", new Dictionary<string, string>(), token);
26+
return line;
27+
} catch {
28+
return null;
29+
}
30+
}
31+
}
32+
}

src/Runner.Client/Program.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,12 @@ await Task.WhenAny(Task.Run(() => {
20022002
}
20032003
}
20042004
}
2005+
if(parameters.Event != "azpipelines") {
2006+
var githubToken = await GhCliAuth.GetToken(null, new TraceWriter(parameters), CancellationToken.None);
2007+
if(githubToken != null) {
2008+
wsecrets.Add($"github_token={githubToken}");
2009+
}
2010+
}
20052011
if(parameters.Secrets?.Length > 0) {
20062012
for(int i = 0; i < parameters.Secrets.Length; i++ ) {
20072013
var e = parameters.Secrets[i];

src/Runner.Server/Controllers/MessageController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ private static void LoadEnvSec(string[] contents, Action<string, string> kvhandl
389389
if (separatorIndex > 0)
390390
{
391391
string envKey = env.Substring(0, separatorIndex);
392-
string envValue = null;
392+
string envValue = "";
393393
if (env.Length > separatorIndex + 1)
394394
{
395395
envValue = env.Substring(separatorIndex + 1);

0 commit comments

Comments
 (0)