简体   繁体   English

Azure DevOps 发布管道,从 C# 代码读取环境变量

[英]Azure DevOps Release Pipeline, Read Environment Variables from C# Code

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).在我的 TestCase 项目中,我将一些设置存储为环境变量,这些设置在我的测试设置过程中被访问,但在 azure 发布管道中它只是返回为 null(这是使用托管代理)。

How can I access the Variables set in Variables tab of a release pipeline from my C# code?如何从我的 C# 代码访问发布管道的“变量”选项卡中设置的变量?

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 .使用EnvironmentVariableTarget.Process

You can use Environment.GetEnvironmentVariable("DevOpsUserName", EnvironmentVariableTarget.Process) , as Daniel mentioned.正如丹尼尔提到的,您可以使用Environment.GetEnvironmentVariable("DevOpsUserName", EnvironmentVariableTarget.Process)

Or you can directly use Environment.GetEnvironmentVariable("DevOpsUserName") , which is equivalent to the previous statement and targets the process -level environment variables.或者你可以直接使用Environment.GetEnvironmentVariable("DevOpsUserName") ,它等同于前面的声明并针对进程级别的环境变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM