amplify-hosting icon indicating copy to clipboard operation
amplify-hosting copied to clipboard

How to set Preview to use PR destination Environment variables

Open cmendes0101 opened this issue 5 years ago • 16 comments

** 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**

cmendes0101 avatar Apr 27 '20 15:04 cmendes0101

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.

cslogan-red avatar Apr 30 '20 23:04 cslogan-red

+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: Screenshot 2020-05-04 at 12 43 14

@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?

fullstackfool avatar May 04 '20 11:05 fullstackfool

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

liflovs avatar Nov 24 '21 22:11 liflovs

+1 for PR env vars. Waiting for this feature

IRubinski avatar Jun 21 '22 10:06 IRubinski

+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.

keremnalbant avatar Oct 26 '22 21:10 keremnalbant

+1 for PR env vars too much manual process to do it per new PR

meehow-m avatar Nov 23 '22 15:11 meehow-m

+1 Waiting this feature

lisbuenas avatar Dec 15 '22 17:12 lisbuenas

+1 for PR env vars x 10000

Any ETA updates on this?

mswezey23 avatar Feb 24 '23 19:02 mswezey23

@cslogan-red This has been pending prioritisation for 3 years. Can you finally prioritise it, please?

borisyordanov avatar Apr 12 '23 14:04 borisyordanov

@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.

cslogan-red avatar Apr 12 '23 15:04 cslogan-red

@hloriii maybe you can assist too?

borisyordanov avatar Apr 18 '23 13:04 borisyordanov

+1

Ddevon avatar Sep 27 '23 00:09 Ddevon

+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)

axtg avatar Oct 13 '23 09:10 axtg

+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

jdevost avatar Feb 13 '24 22:02 jdevost

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:

  1. login to AWS after every pull request
  2. navigate to and override env variable
  3. 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:

  1. set NEXT_PUBLIC_ENV=production on AWS Amplify (all pr branches will inherit it
  2. if USER_BRANCH starts with pr, set env to preview, else get it from env var
const BRANCH = process.env.USER_BRANCH || '';
const APP_ENV = BRANCH.startsWith('pr')
  ? 'preview'
  : process.env.NEXT_PUBLIC_ENV;

bivainis avatar Feb 27 '24 08:02 bivainis

Any ETA on this feature?

BharatNischal avatar Mar 21 '24 11:03 BharatNischal

@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

swaminator avatar Jun 24 '24 18:06 swaminator

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.

github-actions[bot] avatar Jun 24 '24 18:06 github-actions[bot]

This issue has been automatically locked.

github-actions[bot] avatar Jun 24 '24 19:06 github-actions[bot]