Azure / static-web-apps

Azure Static Web Apps. For bugs and feature requests, please create an issue in this repo. For community discussions, latest updates, kindly refer to the Discussions Tab. To know what's new in Static Web Apps, visit https://aka.ms/swa/ThisMonth
https://aka.ms/swa
MIT License
324 stars 56 forks source link

How to pass custom environment variables to SWA Build #1243

Open pcanas opened 1 year ago

pcanas commented 1 year ago

I would like to pass a custom environment variable to be by my React-based PWA. So far, this is how the app looks:

Captura de pantalla 2023-08-10 a las 8 39 42

I followed this tutorial and try to add CUSTOM_ENV and PPP in 2 different ways:

      - name: Build And Deploy
        id: builddeploy
        uses: Azure/static-web-apps-deploy@v1
        with:
          azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_RED_SMOKE_0B4D0EC03 }}
          repo_token: ${{ secrets.GITHUB_TOKEN }} # Used for Github integrations (i.e. PR comments)
          action: "upload"
          ###### Repository/Build Configurations - These values can be configured to match your app requirements. ######
          # For more information regarding Static Web App workflow configurations, please visit: https://aka.ms/swaworkflowconfig
          app_location: "/" # App source code path
          api_location: "api" # Api source code path - optional
          output_location: "build" # Built app content directory - optional
        env: # Add environment variables here
          PPP: ppp

None of the methods work, as you can see from the following screenshot, process.env does not contain neither variable:

Captura de pantalla 2023-08-10 a las 8 46 02

Am I missing anything? How can I pass custom environment variables?

phranger commented 1 year ago

+1

nilokillian commented 12 months ago

+1

mansourmahboubi commented 10 months ago

+1

DavidDeSloovere commented 9 months ago

I'm wondering if there is a difference with backend and frontend. Banckend runs in an Azure Function afaik. Maybe only backend knows about the ENV variables? Just guessing here.

Edit: passing env via the Azure/static-web-apps-deploy@v1 task did work for my Nuxt app. took me a long time to get it working though.

DavidDeSloovere commented 7 months ago

Things like React, Vite, Nuxt etc need the env variables at build time. So you need to specify it in the GH account, and not the Azure portal.

Maybe the fix for the OP @pcanas is that react needs to REACT_prefix? This is the case for Vite (VITE_) and Nuxt (NUXT_).

        env: # Add environment variables here
          REACT_PPP: ppp

So it's not a bug. But more documentation or guidance that is lacking.