2

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.

Kapé
  • 4,411
  • 3
  • 37
  • 54

3 Answers3

2

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).

Alexander Köplinger
  • 2,497
  • 17
  • 15
1

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).

Josmar
  • 584
  • 7
  • 16
0

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

Community
  • 1
  • 1