7

I'm using Azure Static Web App service and Azure DevOps pipeline to deploy a NodeJS app. The pipeline and the build are going well. Now i have to define a URL for the backend using env variables, but without success.

trigger:
- develop

jobs:
  - job: JobTest
    pool:
      vmImage: ubuntu-latest

    variables:
    - name: BACKEND_URL
      value: https://<some_url>
    - name: System.Debug
      value: true
    steps:
    - task: AzureStaticWebApp@0
      inputs:
        app_location: "/" 
        api_location: ""
        output_location: "dist"

      env:
        BACKEND_URL: $(BACKEND_URL)
        azure_static_web_apps_api_token: $(deployment_token)
    - bash: echo $(BACKEND_URL)
    - bash: echo $PWD

and from the NodeJS code, in the "/app/src/models/config.ts" file i have the this:

export const BACKEND_URL = process.env.BACKEND_URL

If i change process.env.BACKEND_URL with the actual URL it will work.

Also, from the Azure Pipeline this task is using the https://github.com/microsoft/Oryx build system.

The question is how can i use env from the pipeline in the code?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
IvanAK
  • 203
  • 2
  • 11
  • I don’t think the task supports additional environment variables right now. Created an issue here to fix https://github.com/Azure/static-web-apps/issues/392 – Anthony Chu Apr 29 '21 at 03:25

2 Answers2

2

Static Web App cannot use back end variables.

You could consider using .env file to config your environment variables. Format like "name=value"(without quotes).

And install dotenv in the file you want to invoke the environment variables, access them by process.env.

Have a look at this article: Node.js Environment Variable Configuration by using env file

And my another answer: https://stackoverflow.com/a/67052708/13586071

Doris Lv
  • 3,083
  • 1
  • 5
  • 14
  • That is how is set up. But i guess its not working with the static app as @Anthony Chu point out. I have `.env.sample` file in `/app/build/` directory. And also i have the bash script `cp ./build/.env.sample .env` to be executed from the `package.json` file in `/app/` directory. Locally works ok but when i try to build it with Azure Pipeline im getting error that the file does not exists. So that is why i put the variable in the env and its there but its never render like that or set it up. – IvanAK Apr 29 '21 at 10:18
  • @IvanAK I have a same problem. Did you find any solutions? – M Karimi Nov 21 '21 at 05:27
  • @M Karimi I didn't find any solutions, till that time if i remember good, was that if you bild and deply like that with that kind of pipeline and directly to web app it makes a problem. Other pipelines or applications dont have it. Try using GitHub actions and work flows – IvanAK Nov 21 '21 at 08:53
2

To help improve this issue, I have added an answer:

the env variables cannot be used azure pipeline with azure Static Web App. And here is a ticket which reports the issue.

Mr Qian
  • 21,064
  • 1
  • 31
  • 41