Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set CI=true when launch process in actions runner. #374

Merged
merged 1 commit into from Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Runner.Sdk/ProcessInvoker.cs
Expand Up @@ -271,6 +271,14 @@ public ProcessInvoker(ITraceWriter trace)
// Indicate GitHub Actions process.
_proc.StartInfo.Environment["GITHUB_ACTIONS"] = "true";

// Set CI=true when no one else already set it.
// CI=true is common set in most CI provider in GitHub
if (!_proc.StartInfo.Environment.ContainsKey("CI") &&
Environment.GetEnvironmentVariable("CI") == null)
{
_proc.StartInfo.Environment["CI"] = "true";
}

// Hook up the events.
_proc.EnableRaisingEvents = true;
_proc.Exited += ProcessExitedHandler;
Expand Down
97 changes: 97 additions & 0 deletions src/Test/L0/ProcessInvokerL0.cs
Expand Up @@ -8,6 +8,7 @@
using GitHub.Runner.Common.Util;
using System.Threading.Channels;
using GitHub.Runner.Sdk;
using System.Linq;

namespace GitHub.Runner.Common.Tests
{
Expand Down Expand Up @@ -81,6 +82,102 @@ public async Task SuccessExitsWithCodeZero()
}
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Common")]
public async Task SetCIEnv()
{
using (TestHostContext hc = new TestHostContext(this))
{
var existingCI = Environment.GetEnvironmentVariable("CI");
try
{
// Clear out CI and make sure process invoker sets it.
Environment.SetEnvironmentVariable("CI", null);

Tracing trace = hc.GetTrace();

Int32 exitCode = -1;
var processInvoker = new ProcessInvokerWrapper();
processInvoker.Initialize(hc);
var stdout = new List<string>();
var stderr = new List<string>();
processInvoker.OutputDataReceived += (object sender, ProcessDataReceivedEventArgs e) =>
{
trace.Info(e.Data);
stdout.Add(e.Data);
};
processInvoker.ErrorDataReceived += (object sender, ProcessDataReceivedEventArgs e) =>
{
trace.Info(e.Data);
stderr.Add(e.Data);
};
#if OS_WINDOWS
exitCode = await processInvoker.ExecuteAsync("", "cmd.exe", "/c \"echo %CI%\"", null, CancellationToken.None);
#else
exitCode = await processInvoker.ExecuteAsync("", "bash", "-c \"echo $CI\"", null, CancellationToken.None);
#endif

trace.Info("Exit Code: {0}", exitCode);
Assert.Equal(0, exitCode);

Assert.Equal("true", stdout.First(x => !string.IsNullOrWhiteSpace(x)));
}
finally
{
Environment.SetEnvironmentVariable("CI", existingCI);
}
}
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Common")]
public async Task KeepExistingCIEnv()
{
using (TestHostContext hc = new TestHostContext(this))
{
var existingCI = Environment.GetEnvironmentVariable("CI");
try
{
// Clear out CI and make sure process invoker sets it.
Environment.SetEnvironmentVariable("CI", null);

Tracing trace = hc.GetTrace();

Int32 exitCode = -1;
var processInvoker = new ProcessInvokerWrapper();
processInvoker.Initialize(hc);
var stdout = new List<string>();
var stderr = new List<string>();
processInvoker.OutputDataReceived += (object sender, ProcessDataReceivedEventArgs e) =>
{
trace.Info(e.Data);
stdout.Add(e.Data);
};
processInvoker.ErrorDataReceived += (object sender, ProcessDataReceivedEventArgs e) =>
{
trace.Info(e.Data);
stderr.Add(e.Data);
};
#if OS_WINDOWS
exitCode = await processInvoker.ExecuteAsync("", "cmd.exe", "/c \"echo %CI%\"", new Dictionary<string, string>() { { "CI", "false" } }, CancellationToken.None);
#else
exitCode = await processInvoker.ExecuteAsync("", "bash", "-c \"echo $CI\"", new Dictionary<string, string>() { { "CI", "false" } }, CancellationToken.None);
#endif

trace.Info("Exit Code: {0}", exitCode);
Assert.Equal(0, exitCode);

Assert.Equal("false", stdout.First(x => !string.IsNullOrWhiteSpace(x)));
}
finally
{
Environment.SetEnvironmentVariable("CI", existingCI);
}
}
}

#if !OS_WINDOWS
//Run a process that normally takes 20sec to finish and cancel it.
[Fact]
Expand Down