Hey! How can I obtain the name of the target branch when creating a preview build in Vercel? My build needs to pass against the target branch so when I merge it, I can be reasonably confident it won’t fail.
Hey,
When creating a preview build in Vercel, you can access information about the target branch through environment variables that Vercel automatically sets for each deployment .
Specifically, Vercel provides the following environment variables that can help you:
VERCEL_GIT_COMMIT_REF
- Contains the branch or tag that the deployment was triggered fromVERCEL_GIT_REPO_OWNER
- The owner of the Git repositoryVERCEL_GIT_REPO_SLUG
- The name of the Git repositoryVERCEL_GIT_PULL_REQUEST_ID
- The ID of the pull request (if applicable)
For your specific use case, you’ll want to know the target branch of your pull request (typically main
). If you need more specific information about the target branch, you might need to use the Git provider’s API (GitHub, GitLab, etc.) within your build script to fetch additional details about the pull request.
# Example of how to use these variables in a build script
echo "Current branch: $VERCEL_GIT_COMMIT_REF"
echo "Pull request ID: $VERCEL_GIT_PULL_REQUEST_ID"
For example, with GitHub, you could make an API call using the VERCEL_GIT_PULL_REQUEST_ID
to get the target branch information.
1 Like