iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🙅‍♀️

Ignoring Preview Deployments for Specific Branches on Vercel

に公開
2
Old content

When you link a GitHub repository to a Vercel project, a preview deployment is created every time you push to any branch. While this is a very convenient feature, frequent pushing can make you worry about hitting limits.

As of March 2021, restricting these preview deployments to specific branches is somewhat complicated. Since there seems to be high demand for this, it might be supported in the future. For the latest information, it's best to check the following issue.

https://github.com/vercel/vercel/issues/3166

How to ignore preview deployments for branches other than specific ones

You will need to use something called "Ignored Build Step". The Zenn development members did this for us. You can get it working by following this scrap.

https://zenn.dev/bisque/scraps/50a51a28d6eb85

1. Place a shell script in your project

As an example, let's say you want to perform a preview deployment only when a push is made to the develop branch (or when a PR is merged). First, create a file such as vercel-ignore-build-step.sh with the following content.

vercel-ignore-build-step.sh
#!/bin/bash

echo "VERCEL_GIT_COMMIT_REF: $VERCEL_GIT_COMMIT_REF"

if [[ "$VERCEL_GIT_COMMIT_REF" == "develop" ]] ; then
  # Proceed with the build
  echo "✅ - Build can proceed"
  exit 1;

else
  # Don't build
  echo "🛑 - Build cancelled"
  exit 0;
fi

2. Configure Ignored Build Step in Vercel Dashboard

Open your project on the Vercel dashboard and navigate to the [Git] menu. In the [Ignored Build Step] section, you can specify a COMMAND to execute the file you created. If you placed vercel-ignore-build-step.sh in the project root, it should look like this:

COMMAND
bash vercel-ignore-build-step.sh


Specify it like this

Settings are now complete. Except when a push is made to the target branch, you will see that it has been cancelled as shown here.

Removing this display seems difficult at the moment...

Extra) How to turn off Vercel Bot comments on Pull Requests

By default, the Vercel Bot is set up to comment on PRs. If you want to turn this off, open vercel.json in your project root (create it if it doesn't exist) and set the value of github.silent to true.

vercel.json
{
  "github": {
    "silent": true
  }
}

👇 Check the comment for the latest method

Discussion

すてぃんすてぃん

古い記事ですが「Vercel branch ignore」などの検索ワードでトップに表示されるのでコメントします。

今は Vercel で特定のブランチ名でデプロイする or しないを決めるもっと簡単な方法があります。
vercel.json で git.deploymentEnabled にブランチ名マッチングルールを指定することで、ブランチへのプッシュでデプロイされるかどうかを決められます。参考になれば幸いです。
https://vercel.com/docs/project-configuration/git-configuration#git.deploymentenabled