Skip to content

Commit

Permalink
Load and set env from .env file before creating HostContext. (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
TingluoHuang authored Dec 12, 2019
1 parent f78d35d commit 2cac011
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions src/Runner.Listener/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -14,6 +15,9 @@ public static class Program
{
public static int Main(string[] args)
{
// Add environment variables from .env file
LoadAndSetEnv();

using (HostContext context = new HostContext("Runner"))
{
return MainAsync(context, args).GetAwaiter().GetResult();
Expand All @@ -25,7 +29,7 @@ public static int Main(string[] args)
// 1: Terminate failure
// 2: Retriable failure
// 3: Exit for self update
public async static Task<int> MainAsync(IHostContext context, string[] args)
private async static Task<int> MainAsync(IHostContext context, string[] args)
{
Tracing trace = context.GetTrace(nameof(GitHub.Runner.Listener));
trace.Info($"Runner is built for {Constants.Runner.Platform} ({Constants.Runner.PlatformArchitecture}) - {BuildConstants.RunnerPackage.PackageName}.");
Expand Down Expand Up @@ -83,22 +87,6 @@ public async static Task<int> MainAsync(IHostContext context, string[] args)
return Constants.Runner.ReturnCode.TerminatedError;
}

// Add environment variables from .env file
string envFile = Path.Combine(context.GetDirectory(WellKnownDirectory.Root), ".env");
if (File.Exists(envFile))
{
var envContents = File.ReadAllLines(envFile);
foreach (var env in envContents)
{
if (!string.IsNullOrEmpty(env) && env.IndexOf('=') > 0)
{
string envKey = env.Substring(0, env.IndexOf('='));
string envValue = env.Substring(env.IndexOf('=') + 1);
Environment.SetEnvironmentVariable(envKey, envValue);
}
}
}

// Parse the command line args.
var command = new CommandSettings(context, args);
trace.Info("Arguments parsed");
Expand Down Expand Up @@ -136,5 +124,34 @@ public async static Task<int> MainAsync(IHostContext context, string[] args)
return Constants.Runner.ReturnCode.RetryableError;
}
}

private static void LoadAndSetEnv()
{
var binDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var rootDir = new DirectoryInfo(binDir).Parent.FullName;
string envFile = Path.Combine(rootDir, ".env");
if (File.Exists(envFile))
{
var envContents = File.ReadAllLines(envFile);
foreach (var env in envContents)
{
if (!string.IsNullOrEmpty(env))
{
var separatorIndex = env.IndexOf('=');
if (separatorIndex > 0)
{
string envKey = env.Substring(0, separatorIndex);
string envValue = null;
if (env.Length > separatorIndex + 1)
{
envValue = env.Substring(separatorIndex + 1);
}

Environment.SetEnvironmentVariable(envKey, envValue);
}
}
}
}
}
}
}

0 comments on commit 2cac011

Please sign in to comment.