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:
- Any push (merge) to
main
should trigger a Production deployment. - Only a PR from a
feature
branch to mainbranch
should trigger a Preview deployment of that feature branch .
Any alternative suggestions on how I could achieve this would be much appreciated.