Kapé
Kapé

Reputation: 4801

How to read Environment variable set by Travis CI in C#

I've created an Unit Test project which requires a key which is not public, so locally I can read it from the App.config but then Travis CI doesn't have this key.

So I've added an Environment Variable in Travis CI like this: Travis CI Environment Variables

When the Travis CI build starts it displays my variable in the log:

Setting environment variables from repository settings
$ export TestKey=TestValue

But according to the log a simple test which reads the key fails with the error message Key not set as environment variable:

[Test]
public void TestTravisEnvVariable()
{
    string testKey= Environment.GetEnvironmentVariable("TestKey");

    Assert.IsNotNullOrEmpty(testKey, "Key not set as environment variable");
}

The Environment.GetEnvironmentVariable method reads the variable of the current process., but apparently the key can't be read in this case.

So how can I actually read this key?

Update: It is not clear to me why it didn't work in the first place but the sample above just works the second time I ran the build.

Upvotes: 2

Views: 1072

Answers (3)

user4399732
user4399732

Reputation:

According to this answer

string keyName = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment\"; 
string existingPathFolderVariable = (string)Registry.LocalMachine.OpenSubKey(keyName).GetValue("Variable You Want To Get", "", RegistryValueOptions.DoNotExpandEnvironmentNames);

Try this

Upvotes: 0

Josmar
Josmar

Reputation: 584

Your build and configuration is fine.

Seems like environment variables configured on the repository usually take some time to be available on builds (unlike .travis.yml env vars).

Upvotes: 1

Alexander Köplinger
Alexander Köplinger

Reputation: 2517

I just tried this on Travis CI and Environment.GetEnvironmentVariable("TestKey"); works fine for me in a simple console app (specifyingEnvironmentVariableTarget.User or .Machine in the overloads doesn't though).

Upvotes: 2

Related Questions