amplify-hosting
amplify-hosting copied to clipboard
How to set Preview to use PR destination Environment variables
** Please describe which feature you have a question about? ** The setup is not using Amplify backend.
We are currently using PR Previews. When building it appears to use the default Environment variables for all branches. When we are making a PR for dev branch we would like the PR Preview to use dev Env's. Is this possible? It makes it harder to test FE dev against production API since they are on different release levels.
** Provide additional details**
Thank you for reporting, you're correct in that each new PR Preview always only inherits environment variables defined for all branches, regardless of the source or target branch. As a workaround, once your PR Preview branch is created in the Console; if your environment variable already exists, you can then manually add branch overrides for your specific PR branch that match whichever branch that you're testing against. We've added this to our backlog as a feature request, pending prioritization.
+1 for PR env vars. We're using amplify cookie-based storage for cognito, and the domain value needs to be set correctly for it to work.
There is already one which is added automatically for each PR, so the principle is sound:

@cmendes0101 One thing that may help is ensuring that the default values for all your env vars are the dev ones. Then overwrite for master branch. That way any PR will target dev, not master.
This of course means that any hotfix's into master will also point to dev api, but do you want to be clicking around your app, altering the live db while testing anyway?
We are using staging (which is then merged into master) and test (per feature) environments for amplify that should point to different backends, we cannot set environmental variables correctly without this feature
+1 for PR env vars. Waiting for this feature
+1 for PR env vars. We are waiting for this feature. We use PR previews to do hotfixes in production. And this is a big problem for people whose default values in the environment variables are the dev ones.
+1 for PR env vars too much manual process to do it per new PR
+1 Waiting this feature
+1 for PR env vars x 10000
Any ETA updates on this?
@cslogan-red This has been pending prioritisation for 3 years. Can you finally prioritise it, please?
@abhi7cr maybe you can take a look if you're still on the project? I'm no longer with AWS or Amplify for the past year or so.
@hloriii maybe you can assist too?
+1
+1 I did go for @fullstackfool's approach. All on dev by default, overwrite for the main branch (which is the only one that exists consistenly)
+1 Just a ENV variable that is set with the destination would be useful. There is already this env var:
AWS_BRANCH=my-feature-branch-used-for-pr-preview
Having this kind of env var also would be useful, as we could use it in build steps:
AWS_BRANCH_DESTINATION=DEV
As a workaround, once your PR Preview branch is created in the Console; if your environment variable already exists, you can then manually add branch overrides for your specific PR branch that match whichever branch that you're testing against.
This works, but because NextJS bundles env vars during build time, the workflow is impaired. One has to:
- login to AWS after every pull request
- navigate to and override env variable
- re-deploy the already deployed preview environment for the env variable to be included in app build
If there was a CLI command to override env vars on Amplify, we could at least set something in GitHub actions to do this automatically, but it doesn't seem to be the case.
EDIT:
Seems like all pr envs have USER_BRANCH env variable named pr.., so if there are no other branches that start with pr, a potential workaround could be:
- set
NEXT_PUBLIC_ENV=productionon AWS Amplify (all pr branches will inherit it - if
USER_BRANCHstarts withpr, set env topreview, else get it from env var
const BRANCH = process.env.USER_BRANCH || '';
const APP_ENV = BRANCH.startsWith('pr')
? 'preview'
: process.env.NEXT_PUBLIC_ENV;
Any ETA on this feature?
@BharatNischal we have a fix for this feature. There are two new env variables for AWS_PULL_REQUEST_SOURCE_BRANCH and AWS_PULL_REQUEST_DESTINATION_BRANCH that can give you the required information during the build. https://docs.aws.amazon.com/amplify/latest/userguide/environment-variables.html#amplify-console-environment-variables
This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.
This issue has been automatically locked.