Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set process.env.var in devops Azure pipeline

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

  • .Net core installer
  • Resore
  • run command (to set env variables)
  • Build
  • publish

Issues:

  1. Environment variable is not set.
  2. I even don't know how to build another artefact for production, but not for development.

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.

like image 593
NILF Avatar asked Oct 28 '19 13:10

NILF


People also ask

How do I set an environment variable in Azure?

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.

How do you set the environment on an Azure pipeline?

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.

How do you use variables in Azure DevOps release pipeline?

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.

What is environment variable in DevOps?

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.

How to set env variables in Azure DevOps pipeline?

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.

How to import a variable into a step in azure pipeline?

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.

How do I get all the environment variables in the pipeline?

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.

What is an environment in Azure DevOps?

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.


1 Answers

TLDR;

  1. You have 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.
  2. But only in the following steps - the env variable is not immediately accessible in the same pipeline step.
  3. You can define variables at pipeline level if you know their values upfront - that should simplify things. task.setvariable is more useful for dynamic variables.
  4. If you need different process (or a different set of variables) for different environments, I recommend using multistage YAML pipelines or classic Releases. They both allow for setting up different stages, each with their set of variables.

Long story

We need to distinguish two separate processes:

  1. Deployment pipeline that's executed on CI agent
  2. Web application that may be hosted in many different ways - Azure Web Apps, self hosting, docker/k8s, etc.

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:

  • It react behaves differently in development and release mode. In Release mode, during the build phase, the whole client-side code is compiled down to a static JS file. So the env variables you set in your pipeline should work.
  • It cannot simply access any env variable (to protect you from exposing your server env variables on client browser by accident). If using 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.
like image 80
qbik Avatar answered Nov 23 '22 13:11

qbik