简体   繁体   English

在 c# 中读取 azure 管道环境变量

[英]reading azure pipeline environment variables in c#

I have set environment variable in azure yaml pipeline like this我已经像这样在 azure yaml 管道中设置了环境变量

- task: PowerShell@2
  inputs:
targetType: 'inline'
script: |      $env:MY_VARIABLE = "$(my-value)"
$env:MY_VARIABLE >> test.txt
...

when I run the pipeline and go to published artifact in summary section of the pipeline, I can see that test.txt has correct value.当我运行管道和 go 以在管道的摘要部分发布工件时,我可以看到 test.txt 具有正确的值。 Is there a way I can read this MY_VARIABLE in c# code, which is going to be run with a specflow task in this yaml pipeline有没有办法我可以在 c# 代码中读取这个MY_VARIABLE ,这将在这个 yaml 管道中使用 specflow 任务运行

You can't read Azure Pipelines variable directly in C#.您无法直接在 C# 中读取 Azure Pipelines 变量。 However, each Azure Pipeline variable is mapped to an environment variable (except for secrets).但是,每个 Azure 管道变量都映射到环境变量(机密除外)。 You can read about this here .您可以在此处阅读相关内容。

So, all that you need is Environment.GetEnvironmentVariable .因此,您所需要的只是Environment.GetEnvironmentVariable

And of course you need to run your code on pipeline, not compile create package and then run somewhere else.当然,您需要在管道上运行代码,而不是编译创建 package 然后在其他地方运行。

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

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