Hi @Minjin Park,
Your Vite environment variables (like VITE_LOGIN_API_URL
) are intended to be used during build time, because Vite inlines them into the final bundle. These variables must be present before the build runs; they are not accessible at runtime like in Node.js (process.env
). In Azure Static Web Apps, the correct way to make environment variables available to Vite is by configuring them in the Azure Portal under Application Settings, not just GitHub secrets.
While you’ve correctly added them under GitHub repository secrets and referenced them in your GitHub Actions YAML under the env:
block, those secrets are not automatically passed to the Azure build environment unless explicitly mapped into the build command which is not necessary in this case.
Set Environment Variables in Azure Portal for your Static Web App:
- Go to your Azure Static Web App resource in the Azure portal.
- In the left menu, go to Configuration => Application settings.
- Add the environment variables exactly like this:
- VITE_LOGIN_API_URL = https://yourdomain.com/auth/login
- VITE_REGISTRATION_API_URL = https://yourdomain.com/auth/register
- Save and restart the app.
These Application Settings are automatically injected into the environment during the Azure-hosted build process (which GitHub Actions triggers by default), making them available to Vite at build time. Configure application settings for Azure Static Web Apps
Trigger a new GitHub Actions deployment after saving your Azure settings. You can do this by: Making any small commit and pushing or manually triggering the workflow.
you're already using the correct syntax in your code: import.meta.env.VITE_LOGIN_API_URL
. Just ensure that these variables are present at build time via Azure's Application Settings. Vite does not support injecting these dynamically at runtime, if they’re missing at build time, they’ll be undefined
in your code.
https://learn.microsoft.com/en-us/azure/static-web-apps/configuration#application-settings
https://learn.microsoft.com/en-us/azure/static-web-apps/build-configuration?tabs=identity&pivots=github-actions
Hope this information is helpful, let me know if you have any further queries.