Azure DevOps - Setting An Environment Variable on the Target Machine in a Deploy Group - azure

I am deploying an Asp.Net Core application via Azure DevOps' Pipelines option and trying to set an environment variable on each of the target machines. Each deployment environment has its own settings file (for example "appsettings.Development.json") that the Asp.Net app chooses from when it starts up by reading the ASPNETCORE_ENVIRONMENT environment variable. I am using a Deploy Group to deploy the artifact to our on-site servers and that is working perfectly until it comes to setting the correct appsettings file to use.
I tried unsuccessfully to use a PowerShell task that has this inline script:
$Env:ASPNETCORE_ENVIRONMENT="$(EnvironmentName)"
All the pipeline tasks are executed successfully but the environment variable doesn't actually get applied on the target machine.
How can I set an environment variable on the target machines in a Deploy Group?

The above inline script of yours $Env:ASPNETCORE_ENVIRONMENT="$(EnvironmentName)" only set the variable in the current terminal window opened by the powershell task. The terminal window will be shut down after the powershell task is complete. We usually use below solutions to set environment variables in the pipeline.
If you want to set an environment variable on the target machines in the release pipeline. You can directly set a variable ASPNETCORE_ENVIRONMENT in the release pipeline Variables tab. See below:
Go to your release Edit page-->Variables tab-->Add new Variable-->Set the stage Scope
There is another way to set the environment variables dynamically using logging commands(Write-Host "##vso[task.setvariable..]) in the pipeline. See below inline script in powershell task:
echo "##vso[task.setvariable variable=ASPNETCORE_ENVIRONMENT]$(EnvironmentName)"
Please check the official document for more information.

Setting an environment variable that way isn't persistent, as you've observed. You'll need to the .NET environment variable methods to accomplish what you want.
[System.Environment]::SetEnvironmentVariable('ASPNETCORE_ENVIRONMENT', '$(EnvironmentName)' [System.EnvironmentVariableTarget]::Machine)

Related

Pass pipeline variable to release variable Azure DevOps

Created CICD pipeline with trigger release, and Pipeline variable set in to Azure Devops Rest API. Getting variable in pipeline and set Output true using PowerShell.
Write-Host $(webAppName)
echo "##vso[task.setvariable variable=webAppName;isoutput=true]$webAppName"
And set the release pipeline variable using
WebAppName= ${{variables.webAppName}} on the variable tab. It's not working, don't have idea how can we retrive the pipeline variable in release.
The is no official way to pass variables from Build to Release.
Using Variable Groups in Azure Pipeline Library is a good option: https://learn.microsoft.com/en-us/azure/devops/pipelines/library/variable-groups?view=azure-devops&tabs=yaml
Another work around, you could store the variable values in a file and attach that as a Build Artifact. That way you can read the file in the release pipeline by downloading Build Artifacts and set the variable again. You could check my answer here: is there any way to pass variables from CI to CD pipeline

Azure pipelines secret variable and react app deployment

I have been trying for a few days on how to use an environment secret variable set in azure pipelines.
Here is source code where I get the environment variable value.
Here is the .env file I used with the value
Below is the TEST word successfully shown when I try to run in vscode via yarn start.
I setup environment variable and set variable as secret in the azure pipeline.
I followed some microsoft tutorial as well as some other answers here in stackoverflow.
I setup powershell task in azure pipelines to get secret variable.
All task run successfully. All task for IIS Deployment also run successfully.
But when I open the web app in the II Server, the result is TEST is not displayed at all.
Heading
If I use the pipeline variable as plaintext instead of secret, TEST is successfully displayed.
Did someone encountered this before and solved it?

Environments are not automatically created in Azure Devops when declared in yaml pipeline config

I'm new to Azure Devops. I would like to have devops pipeline environments to be created automatically during pipeline flow. So the 5th line below should create environment if it does not exist:
- deployment: Deploy
displayName: Deploy job
pool:
vmImage: $(vmImageName)
environment: 'production'
Instead I'm getting:
What am I missing?
To automate environment creation I could also use Terraform but this time I cannot find terraform resource config responsible for that.
I had a similar problem and found that the documentation lists some possible reasons for why this can happen:
Quote from learn.microsoft.com:
Q: Why am I getting error "Job XXXX: Environment XXXX could not be
found. The environment does not exist or has not been authorized for
use"?
A: These are some of the possible reasons of the failure:
When you author a YAML pipeline and refer to an environment that does not exist
in the YAML file, Azure Pipelines automatically creates the
environment in some cases:
You use the YAML pipeline creation wizard in the Azure Pipelines web
experience and refer to an environment that hasn't been created yet.
You update the YAML file using the Azure Pipelines web editor and save
the pipeline after adding a reference to an environment that does not
exist.
In the following flows, Azure Pipelines does not have
information about the user creating the environment: you update the
YAML file using another external code editor, add a reference to an
environment that does not exist, and then cause a manual or continuous
integration pipeline to be triggered. In this case, Azure Pipelines
does not know about the user. Previously, we handled this case by
adding all the project contributors to the administrator role of the
environment. Any member of the project could then change these
permissions and prevent others from accessing the environment.
If you are using runtime parameters for creating the environment, it
will fail as these parameters are expanded at run time. Environment
creation happens at compile time, so we have to use variables to
create the environment.
A user with stakeholder access level cannot create the environment as
stakeholders do not access to repository.
In our case, the problem was using runtime parameters for creating the environment.
You have the environment name 'production' hardcoded, so your problem might be related to one of the other cases.

Azure Task Group deployment through release - How to get Azure Function App deployment output url 'AppServiceApplicationUrl' for later smoke test task

I've created a task group which deploys an Azure Function App which normally in a normal release pipeline (not a task group), it gives you the option of naming this variable on the panel which opens on the right hand side:
output variable
But when an azure function app task is contained within a task group, it gives you no option to create an output variable.
I was wondering if theres a way I can capture that output variable so I can later use it as a variable in a later task within the same task group?
You will find your variable but under slightly different name.
Assuming you have one FunctionApp in yout ask group you will get it via this variable
AZUREFUNCTIONAPP_APPSERVICEAPPLICATIONURL
To be sure please display all env variables. For Ubuntu you can add bash script with this command
env | sort
For more information please check this GitHub issue.

In azure devops: how can I inject my pipeline variables as appsettings when deploying a docker container to azure?

I'm having trouble setting my appsettings in a deployed docker container on azure.
My setup:
I have a .NET core app
My build pipeline builds a docker image and pushes it to my container registry on azure.
My release pipeline pulls the image based on a tag and deploys it to an azure web app.
I need to deploy the image to multiple environments. Every environment has different appsettings. I defined the variables in my pipeline "variables tab":
And I need to send these to my azure so they can be used.
When I manually add them it works, but i want to extract them from my variables, so I only have to add them once. (see screenshot 1)
Edit: The screenshot above works. But this is not what I'm looking for. As I'd have to edit the appsettings pipeline each time I add or remove a new appsetting. Also I believe that removing an appsetting here will just leave it on the deployed environment.
I'm deploying an existing docker image, so i'm unable to edit the appsetting.json file. I also won't make different docker files for each environment.
Is there a way to achieve this? How can I extract / list the variables defined in my pipeline as docker variables or appsettings?
You can define pipeline variables in your pipeline and have them attached to a specific scope (read stage) or the release scope (applied to all stages).
E.g. I have a variable defined as EnvironmentConnectionString which is defined in two scopes:
Scope Test: "EnvironmentConnectionString = server=test-db; ...."
Scope QA: "EnvironmentConnectionString = server=qa-db;..."
Score Release: "logging_flag=enabled"
Then you can set this up in your "Application and Configuration Settings" like
- ConnectionString $(EnvironmentConnectionString)
- Logging $(logging_flag)
Note the $(variable name) syntax for using these variables
When the different stages of the pipeline run, they automatically pick up the values specific to the stage and apply to azure app settings.
You can have different variable groups for different stages. These Variable Groups should have same variables defined with different values.
For example: The Dev Variable Group and Release group both have variables Port, RequestTimeout... The Port in Dev is 4999 while the Port in Release could be 5000. We can link these groups to specific Stage scope, Dev variable group for Dev stage and Release group for Release stage.
[![enter image description here][1]][1]
Make sure all your stages have same settings like this, and then the variables with be replaced with correspondings values for different scopes.
Update:
Each stage in the pipeline is independent, they represent different environments. So we have to define the settings of stage or settings of the tasks within the stages one by one. We have to define the appsettings input one by one.
[1]: https://i.stack.imgur.com/ukbjs.png

Resources