I have this code in the program.cs
file - how to pass the value from the Azure pipeline to the TANKDEV_ENV?
string environment = Environment.GetEnvironmentVariable("TANKDEV_ENV") ?? "Development";
Pass the STG or production or development from the pipeline
I have this code in the program.cs
file - how to pass the value from the Azure pipeline to the TANKDEV_ENV?
string environment = Environment.GetEnvironmentVariable("TANKDEV_ENV") ?? "Development";
Pass the STG or production or development from the pipeline
Share Improve this question edited 2 days ago marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked 2 days ago AlgoXperience -TamilAlgoXperience -Tamil 11 silver badge New contributor AlgoXperience -Tamil is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 2- 1 I've removed the irrelevant tags from your question. Please be mindful of the tags you're using and if they have any bearing on the specific question you're asking. – ProgrammingLlama Commented 2 days ago
- I've not used Azure Pipelines, so I'm not sure if these docs are useful: learn.microsoft/en-us/azure/devops/pipelines/process/… – ProgrammingLlama Commented 2 days ago
1 Answer
Reset to default 2You can use pipeline variables to pass values as environment variables into the C# app when executing the C# App in pipleine. In pipleine, the normal pipeline variables (not secret variables) will be automatically decrypted into environment variables. So, you can define some normal pipeline variables with the values you want to pass from pipleine into the C# app.
See below example as reference:
- The C# code (a Console App).
var my_var01 = Environment.GetEnvironmentVariable("MY_VAR01", EnvironmentVariableTarget.Process);
var my_var02 = Environment.GetEnvironmentVariable("MY_VAR02", EnvironmentVariableTarget.Process);
var my_var03 = Environment.GetEnvironmentVariable("MY_VAR03", EnvironmentVariableTarget.Process);
var my_var04 = Environment.GetEnvironmentVariable("MY_VAR04", EnvironmentVariableTarget.Process);
Console.WriteLine($"my_var01 = {my_var01}.");
Console.WriteLine($"my_var02 = {my_var02}.");
Console.WriteLine($"my_var03 = {my_var03}.");
Console.WriteLine($"my_var04 = {my_var04}.");
- The pipeline (azure-pipelines.yml).
. . .
# Global pipeline variables.
# Can be available for all stages, jobs and tasks within current pipeline.
variables:
my_var01: 'VarValue01' # The environment variable 'MY_VAR01' is automatically mapped for it.
stages:
- stage: Stage1
# Stage-level pipeline variables.
# Can be available for all jobs and tasks within current stage.
variables:
my_var02: 'VarValue02' # The environment variable 'MY_VAR02' is automatically mapped for it.
jobs:
- job: JobA
# Job-level pipeline variables.
# Can be available for all tasks within current job.
variables:
my_var03: 'VarValue03' # The environment variable 'MY_VAR03' is automatically mapped for it.
steps:
# Set pipeline variable when running the task.
# Can be available for all the susequent tasks within current job.
# The environment variable 'MY_VAR04' is automatically mapped for 'my_var04'.
- pwsh: |
$getValue = "VarValue04"
Write-Host "##vso[task.setvariable variable=my_var04;]$getValue"
displayName: 'Set Variable'
- task: DotNetCoreCLI@2
displayName: 'Build App'
inputs:
command: 'publish'
publishWebProjects: false
projects: 'RetrieveEnv/RetrieveEnv.csproj'
arguments: '-c $(BuildConfiguration) -o $(Build.ArtifactStagingDirectory)'
zipAfterPublish: false
- task: DotNetCoreCLI@2
displayName: 'Execute App'
inputs:
command: 'custom'
custom: '$(Build.ArtifactStagingDirectory)/RetrieveEnv/RetrieveEnv.dll'
If the values you want to pass into the C# app are static/fixed and will not be changed often, you can use the values to define some static pipeline variables, just like as the variable my_var01
, my_var02
or my_var03
in above example.
If the values are generated/fetched during running the pipeline, for example, when you run some command lines on the script task to output some values, you can use the logging command SetVariable to set variables with the fetched values. Just like as the variable my_var04
in above example.