I have a task to deploy aspnet core React application to 2 different environments: development and production environments. Each of this environments should be configured separately.
I use Azure devops for CI/CD
AspNet project contains following commands for building application
<Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build" />
I use adal for authorization that is why I have to pass some secret variables that are different for Dev and Prod
const adalConfig = {
tenant: process.env.REACT_APP_TENANT,
clientId: process.env.REACT_APP_CLIENT_ID,
redirectUri: process.env.REACT_APP_REDIRECT_URI,
In Azure devops I set params with command:
echo ##vso[task.setvariable variable=REACT_APP_TENANT;isOutput=true]c00000-00ce-000-0f00-0000000004000
in the azure devops I have next standard commands for aspnet core build app
Issues:
Maybe you already had task to deploy core react app to 2 different environments? Or please give advice if I need to change deployment strategy at all.
The only solutions what I found is to use .env file but I have to commit this file to git - to deploy it from master. And I still don't know how to use different files for dev and prod.
To set environment variables when you start a container in the Azure portal, specify them in the Advanced page when you create the container. Under Environment variables, enter NumWords with a value of 5 for the first variable, and enter MinLength with a value of 8 for the second variable.
There are 3 ways to get an environment variable value on your build server: Set the value on the build machine. Set the value in the YAML build script. Set the value in Azure DevOps for the build pipeline definition.
You define and manage these variables in the Variables tab in a release pipeline. In the Pipeline Variables page, open the Scope drop-down list and select "Release". By default, when you add a variable, it is set to Release scope. Share values across all of the tasks within one specific stage by using stage variables.
Environment variables are pairs of key and value that can be used to customize the build process and store sensitive data such as access details to deployment servers.
Create your variables in your Azure DevOps pipeline and provide those variables the values. Create a Powershell script that you will run in the beginning to set your Env Variables.
When importing a variable into a step in you devops script you can use env or variables. The env parameter is used when importing variables as secrets from your library. Azure pipelines will avoid printing the values in logs.
If you’re building on a linux build agent you can use the printenv command to get a list of all the environment variables in the devops script. You can also use set to get all the environment information. This will also work on windows build agents. If you set the variable System.Debug to true you will get a full debug stream in the pipeline logs.
Azure Pipelines | Azure DevOps Server 2020 An environment is a collection of resources, such as Kubernetes clusters and virtual machines, that can be targeted by deployments from a pipeline. Typical examples of environment names are Dev, Test, QA, Staging, and Production. The advantages of using environments include the following.
isOutput=true
in your task.setvariable
command. This only sets a variable in Pipelines engine, to be available to other steps, but doesn't actually map to an env variable. Remove isOutput
and you shall see REACT_APP_TENANT
env variable.task.setvariable
is more useful for dynamic variables.We need to distinguish two separate processes:
Doing echo ##vso[task.setvariable ...]
sets the variable in the pipeline (1.).
The place where reading variables takes place (like tenant: process.env.REACT_APP_TENANT
) isn't that obvious. If it's nodejs server-side code, it'll be executed in 2. If it's a part of some build script, it'll be read in 1.
React is tricky, because:
create-react-app
(which is what ASP.NET React App does by default), you have to prefix env variables with REACT_APP_
to use them. If using Webpack directly, you'll need some plugin for this.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With