Reputation: 3253
I have set environment variable in azure yaml pipeline like this
- 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. 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
Upvotes: 0
Views: 5011
Reputation: 1
string var_calling = Environment.GetEnvironmentVariable(variable_name_same_as_yml_file,EnvironmentVariableTarget.Process);
Upvotes: 0
Reputation: 3253
Found that by using env:
in task like below we can have compiled C# code ( in my case running on ms hosted agent) read the variables directly.
- task: VSTest@2
displayName: tests
env:
MY_VARIABLE = "$(my-value)"
and then in c# code we can do like this
string var= Environment.GetEnvironmentVariable("MY_VARIABLE", EnvironmentVariableTarget.Process);
Note that without using env:
, it may not work
Upvotes: 1
Reputation: 40643
You can't read Azure Pipelines variable directly in C#. However, each Azure Pipeline variable is mapped to an environment variable (except for secrets). You can read about this here.
So, all that you need is Environment.GetEnvironmentVariable
.
And of course you need to run your code on pipeline, not compile create package and then run somewhere else.
Upvotes: 1