Unable to read dynamic '.env' variables post the Replace tokens in the Azure DevOps Release pipeline

104 Views Asked by At

I need to replace .env variables in the Azure DevOps release pipeline. I have used 'Replace tokens' task for the same. I have verified that the task is able to replace my variables from the variable group, but still, the application is not able to read the deployed variables. Adding screenshots below for more understanding.

Here is my CI build pipeline

enter image description here

.env file from the code

enter image description here

I use replace tokens to replace the env variables from the artifact drop

enter image description here

Task to show content post the replacement of variables

enter image description here

Logs confirming tokens replaced in the task

enter image description here

show content logs confirming the replaced variables

enter image description here

But, when I run the application for making an api call, I still see Not replaced token variable in the console.log. I'm reading the variables in the code using process.env.{VariableName}

enter image description here

I think the variables are getting replaced in the .env file but as the .env file is not a part of the build folder which gets deployed, the changes are not reflecting in the target environment. Could someone help here?

1

There are 1 best solutions below

0
Alvin Zhao - MSFT On

Based on your description, you built your NodeJS app and then updated the .env file for deployment. You may check the file contents by accessing Kudu for your app

enter image description here

Once confirmed the deployed .env file had the updated contents, it meant the deployment was succeeded, but your app couldn't read the .env file.

For this, you may run the Replace Token task in your CI pipeline before npm build or you may test to run startup command after deployment.