I thought the variables set in the Variables tab in a release pipeline were set as environment variables?
On my TestCase Project I've got some settings stored as environment variables which are accessed during setup process of my tests, but in azure release pipeline it just comes back as null(this is using a hosted agent).
How can I access the Variables set in Variables tab of a release pipeline from my C# code?
My code to access Environment variables at the moment is
Environment.GetEnvironmentVariable("DevOpsUserName", EnvironmentVariableTarget.Machine)
This doesn't pull back the variables data.
They are set as environment variables (excepting secret variables, which are not automatically converted to environment variables for security reasons).
However, they're not machine-level environment variables. They are process-level.
Use EnvironmentVariableTarget.Process.
You can use Environment.GetEnvironmentVariable("DevOpsUserName", EnvironmentVariableTarget.Process), as Daniel mentioned.
Or you can directly use Environment.GetEnvironmentVariable("DevOpsUserName"), which is equivalent to the previous statement and targets the process-level environment variables.
Related
I have been using c# code to get blob items for the past few days, however, with no changes to the way the program gets the blob data, it stopped working. I run into the same error every time I run now:
"EnvironmentCredential authentication unavailable. Environment variables are not fully configured"
Here is the code I am using to connect to Azure:
Uri accountUri = new Uri(mystorageurl);
BlobServiceClient client = new BlobServiceClient(accountUri, new DefaultAzureCredential(true));
BlobContainerClient container = client.GetBlobContainerClient(blobname);
BlobClient bundle = container.GetBlobClient(itemname);
What I've been confused by is that if I run this same code in a separate vs solution, I get no error getting the files from Azure. I've also tried sending the same solution that's getting the error to another person and they were able to run it without issue. I know it isn't an issue with environment variables, since it used to work up until now and they haven't been modified in any way.
This unresolved issue on github is most similar to what I've encountered:
https://github.com/Azure/azure-sdk-for-net/issues/16079
It worked fine when you never set Environment variables, it means that you didn't use EnvironmentCredential. The DefaultAzureCredential will attempt to authenticate via the following mechanisms in order, and Environment is the first one.
If you just use Environment to authenticate, it's better to use EnvironmentCredential instead of DefaultAzureCredential. And it's necessary to set the following variables.
AZURE_CLIENT_ID: id of an Azure Active Directory application
AZURE_TENANT_ID: id of the application's Azure Active Directory tenant
AZURE_CLIENT_SECRET: one of the application's client secrets
I have a c# application that runs in the Kubernetes cluster. my application pods have to read the values from azure keyvalut. Currently, I'm using CSI driver and read the values from the filesystem.
Is there any option to set key valut values as environment variables and read?.
You can look into this: https://github.com/SparebankenVest/azure-key-vault-to-kubernetes
it can be used, this open-source project sync the key values from the vault to Kubernetes secrets and you can inject that secret inside the pod as the environment variables.
your application directly reads the value of a key from the environment based on language.
for example with python :
import os
print(os.environ['TEST'])
you can find the documentation at : https://akv2k8s.io/how-it-works/
I cannot get my .NET Core 3.1 console application to recognize my custom system environment variables. I can pull and print other system variables such as username just fine. But if I set a custom one like 'TestKey' in the example below, they are always null within the application.
Here is my code:
static void Main()
{
var config = new ConfigurationBuilder().AddEnvironmentVariables().Build();
var value = config.GetValue<string>("TestKey");
var envValue = Environment.GetEnvironmentVariable("TestKey");
Console.WriteLine($"Config Variable is: {value}");
Console.WriteLine($"Environment Variable: {envValue}");
}
This is the output:
and system variables clearly show the variable has been set:
Likewise, when I do an echo from the command prompt for the TestKey variable, it correctly returns the associated value.
This is a Windows 10 Pro computer and it is not part of a domain. This behavior is truly puzzling. I have rebooted the computer and the system variable persist but still will not appear in the application.
There are two major points here. First, according to Configuration in ASP.NET Core, environment variables are retrieved only for current process
Are only set in processes launched from the command window they were
set in
So, you should set them in the same command window before launch the app or add to the launchsettings.json file
"environmentVariables": {
"Test": "Test",
"ASPNETCORE_ENVIRONMENT": "Development"
}
and restart Visual Studio for that. Or just change it in Debug properties of your project
Second, to be able to get a system environment variable in GetEnvironmentVariable method, you should specify the EnvironmentVariableTarget.Machine parameter
var envValue = Environment.GetEnvironmentVariable("Test", EnvironmentVariableTarget.Machine);
By default
The GetEnvironmentVariable(String) method retrieves an environment
variable from the environment block of the current process only
Or, if you'll add the variable to launchsettings.json in previous point, setting the target isn't needed.
However, if you will run your project in command line via dotnet run, you should be able to access the system environment variable as well, because
On Windows systems, the environment block of the current process
includes:
All environment variables that are provided to it by the parent process that created it. For example, a .NET application launched from
a console window inherits all of the console window's environment
variables.
If there is no parent process, per-machine and per-user environment variables are used instead. For example, a new console
window has all per-machine and per-user environment variables defined
at the time it was launched.
Bottom Line: I had to reboot my web server.
I had this very same problem, in a razor view i had this...
#inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment env
.
.
<div>
Environment is #env.EnvironmentName
</div>
<environment include="Development">
<strong>You are in the Development environment</strong>
</environment>
and in ASPNETCORE_ENVIRONMENT variable i had 'Development'
The page never saw 'Development' it always defaulted to 'Production' (which it's supposed to). Rebooting the web server 'fixed' it for me. I'm not sure if it was the reboot or I just could have recycled the app pool. I'm using .net core 3.1
use the following command to set the environment variable permanently setx key value /M
If you use set command then it will accessible in that cmd.exe itself.
Event after using setx command if the env variable gets null, do a quick restart and it will work.
We have Azure Functions (V2) that have been created with the Service Bus Trigger.
[FunctionName("MyFunctionName")]
public static async Task Run(
[ServiceBusTrigger("%MyQueueName%", Connection = "ServiceBusConnectionString")]
byte[] messageBytes,
TraceWriter log)
{
// code to handle message
}
The queue name is defined in the local.settings.json file:
{
"Values": {
...
"MyQueueName": "local-name-of-my-queue-in-azure",
...
}
}
This works quite well as when deployed we can set the environment variables to be dev-queue-name, live-queue-name etc for the various deployed environments that we have.
However, when more than one developer is connected locally, given that the local.settings.json file is in source control and needs to be to properly maintain the environment variables, then the local function app runners will all connect to the same queue, and it is random as to which developer's application will pick up and process the messages.
What we need is for each developer to have their own queue, but we do not want to have to remove the JSON config file from source control so that we can maintain a different file (as it contains other pieces of information that need updating).
How can we get each developer / computer running our application to have a unique queue name (but known so that we can create the service bus queues in the cloud)?
You can override the setting value via Environment variables. Settings specified as a system environment variable take precedence over values in the local.settings.json file. Just define an Environment variable called MyQueueName.
Having said that, I think that committing local.settings.json to source control is generally not recommended. I suppose you also store your Service Bus connection string there, which means you store your secrets in source control.
Note that default .gitignore file has it listed out.
If you need it in source control, I would commit a version of local.settings.json with all variables with fake values, and then make each developer setup the proper values locally and then ignore the changes on commit (set assume-unchanged).
I have a ClickOnce application and a simple c# launcher that starts the ClickOnce application by launching IE with the URL of the app. I would like to set an environment variable that can be read by the ClickOnce application. Is this possible? This is my code for the launcher app:
ProcessStartInfo pInfo =
new ProcessStartInfo("iexplore.exe", "http://svr:8080/cotest.application");
Process appProc = new Process();
appProc.StartInfo = pInfo;
appProc.StartInfo.EnvironmentVariables.Add("some_var", "some_val");
appProc.StartInfo.UseShellExecute = false;
appProc.Start();
I realise that I'm setting the variable for iexplore, but I thought (hoped) the environment might be inherited by the ClickOnce app. What happens is that the ClickOnce app starts but if I list its environment, the variable "some_var" is not present.
Any ideas?
Probably you are going to face some security limitation by using ClickOnce when you are changing environmental variables for non user variables.
Please see below:
Edit environment variable in registry
I think you may need to either consider using other setup package methods (i.e. InstallShield) or change the way that you intract with Internet Explorer and pass parameters to your site.
If you provide your full requirements people might be able to help you with
alternative solutions.