Giter Club home page Giter Club logo

preview-branches-with-vercel's Introduction

Preview Branches for every Vercel Preview Deployment

This is an example project that shows how you can create a branch for every preview deployment on Vercel.

How it works

There are two workflows in this project:

  • deploy-preview.yml - this is triggered when a Pull request is opened or when new commits are pushed to the branch. It creates a Neon branch and creates a preview deployment for every commit. The workflow also runs Prisma Migrate after creating the branch to apply any pending migrations.
  • deploy-production.yml- this is triggered when you push commits to the main branch (so after merging a pull request). It applies any pending migrations and then deploys the app to production. It also deletes the Neon branch that was created for the preview deployment.

Prerequisites

For the workflows to work, you need to add the following secrets to your GitHub repository:

  • VERCEL_TOKEN
  • NEON_API_KEY - this is the API key for the Neon user that will be used to create the branches. You can find it in the account settings.
  • NEON_PROJECT_ID - this is the ID of the Neon project. You can find it in the project settings on Neon.
  • NEON_MAIN_BRANCH_NAME - this is the name of your primary branch on Neon and what you use for production. By default, it's main
  • PG_DATABSE - this is the name of the database on Neon. By default, it's name is neondb
  • PG_USERNAME - username of the database role

If you're unfamiliar with how to add secrets to a GitHub repository, you can read more about in GitHub's documentation.

Ignored build step

Since we're creating preview deployments using GitHub actions, it's a good idea to disable the build step on Vercel.

set up project locally

If you want to use this project as a playground, you can you can set it up locally.

  1. Clone this repo
git clone
  1. Install dependencies
npm install
  1. Copy the .env.example file to .env:
cp .env.example .env
  1. Add the database credentials:
DATABASE_URL = "" # the pooled connection to the database. It has a `-pooler` suffix
DIRECT_DATABASE_URL = "" # the direct connection string to the database
  1. Run the setup script which creates the tables and runs a seed script:
npm run setup
  1. Deploy the project to Vercel

  2. Open a pull request and see the preview deployment in action

preview-branches-with-vercel's People

Contributors

m-abdelwahab avatar raoufchebri avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

preview-branches-with-vercel's Issues

Preview branches and next.js middleware

Steps to reproduce

The steps in this blog post worked until I start using Next.js middleware. I have tried removing the middleware.ts file and it works again. For some reason, it appears that it is not able to use DATABASE_URL from .env and is trying to force it from Vercel Environment Variables. If I have no DATABASE_URL set for preview in Vercel, I get the following:
Error: No database connection string was provided to neon(). Perhaps an environment variable has not been set?
If I set it, it is using this URL instead.

I have also tried manually setting DATABASE_URL inside the created .vercel/.env.preview.local file with no success.

Expected result

To be able to create preview branches when using middleware.

Actual result

500: INTERNAL_SERVER_ERROR
Code: EDGE_FUNCTION_INVOCATION_FAILED

Is it possible to create preview branches in Vercel whilst using middleware?

Questions about Neon branches in your GitHub Actions workflows

Hello @m-abdelwahab! I was on the call with you earlier today. Thanks for the link, this is super helpful. I had some questions about the values you're using from the GitHub context in your actions:

Could you help me understand why you're using github.pull_request.base.sha and then github.event.before? My understanding is that for a commit on a pull request, github.pull_request.base.sha is the head sha of the base branch of the pull request at the time of the commit, so I'm wondering why you'd want to name your Neon branch after the head sha of the base branch. Is it perhaps supposed to be github.pull_request.head.sha?

And for github.event.before, doesn't that mean you're only deleting the most recently created Neon branch (the one from the last pushed commit) when merging a pull request? Wouldn't that leave a bunch of Neon branches that won't be deleted?

How does the Vercel preview environment get the right `DATABASE_URL`?

Hey folks! Not really a bug report, more of a question. I got here by reading the blog post, but am unable to locate exactly how a specific Vercel preview deployment becomes aware of the DATABASE_URL (for this specific Neon branch) it should connect to? Does the appending DATABASE_URL to .env during migrate get merged with the .env file pulled in with vercel pull?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.