How do I set preview app URLs in NEXT_PUBLIC_APP_URL environment variable?
Last updated: September 12, 2025
Context
When working with preview environments in Porter, you may need to set the preview app URL as an environment variable (NEXT_PUBLIC_APP_URL) for your application. This is particularly useful when working with Next.js applications that require the app URL to be available at build time.
Answer
To set the preview app URL in your NEXT_PUBLIC_APP_URL environment variable, you'll need to follow a two-deployment process when the PR is first opened:
Deploy your PR branch initially
Retrieve the preview domain by running:
porter app run -x <pr-branch> <app-name> -- env | grep PORTER_DOMAINSThis will return a comma-separated list of domains assigned to your app.
Set the environment variable using the retrieved domain:
porter env set -x <pr-branch> -a <app-name> NEXT_PUBLIC_APP_URL=<domain-from-previous-step>This will trigger a second deployment with the new environment variable.
Note: After this initial setup, subsequent deployments will only require a single deployment as the environment variable will be preserved.