Limiting Preview deployments to open PRs

Hi,

I am trying to limited what should trigger a deployment when it comes to pushes to my GitHub project, for a Vite + React app.

Standard behaviour being, any push to any branch on the project triggers a deployment to different environments. I only have the main branch for the “Production” environment, and create feature/* branches ad-hoc, all of which fall under the “Preview” environment.

I set up a bash script which is supposed to still allow production deployments whenever something gets pushed to main(in my case I only allow feature branches to be merged into main), and preview deployments only when a PR from a feature branch into main gets created.

if [[ "$VERCEL_ENV" == "production" && "$VERCEL_GIT_COMMIT_REF" == "main" ]]; then

  exit 1 

fi

if [[ "$VERCEL_ENV" == "preview" && -n "$VERCEL_GIT_PULL_REQUEST_ID" ]]; then

exit 1 

fi

exit 0

I realised that $VERCEL_GIT_PULL_REQUEST_ID is an empty string when a RP is first opened, so the above script concludes with exit 0 at first, and only when something gets pushed to the feature branch after the PR has been opened would it exit 1.

So as it stands, I cannot think of a way to achieve the desired outcome of:

  1. Any push (merge) to main should trigger a Production deployment.
  2. Only a PR from a feature branch to main branch should trigger a Preview deployment of that feature branch .

Any alternative suggestions on how I could achieve this would be much appreciated.

Managed to work around this race condition issue by simply handling it via a GH Actions workflow, similar to this post.

I expanded a bit on the examples given, but it allowed me to be more specific in what I wanted and didn’t want to be deployed.