How to Deploy a Project with Private Git Submodules to Vercel

2024-05-27 00:38 (6 months ago) ytyng

When building with Vercel, you cannot retrieve submodules that are set as private repositories.

Even if you grant permissions to the Vercel app on Github, it won't work.

You need to retrieve them using an HTTP Fine-granted personal access token.

The key point is to execute the following during the build:

git submodule set-url <my-submodule> "https://${GITHUB_PAT}@github.com/ytyng/<my-submodule>.git"
git submodule sync
git submodule update --init

1. Create a Fine-granted personal access token

Go to the following page: https://github.com/settings/tokens?type=beta

Click on "Generate new token."

Image

For Repository access, select "Only select repositories" and choose the minimum necessary repositories.

For Repository permissions, grant Read-only access to Contents only.

Image

Click the "Generate token" button to create the token.

2. Register the token as an environment variable in Vercel

Register it in the Settings → Environment Variables section of your Vercel project. I registered it under the name GITHUB_PAT.

Image

3. Create a build script

Create a file named sh/build-for-vercel.sh.

#!/usr/bin/env bash

cd $(dirname $0)/../ || exit

if [ -z "${GITHUB_PAT}" ]; then
  echo "The environment variable GITHUB_PAT is not set. Please regenerate the Vercel submodule token on Github and register it as the GITHUB_PAT environment variable in Vercel."
  echo "https://github.com/settings/tokens?type=beta"
  echo "https://vercel.com/<my-own-projects>/<project-name>/settings/environment-variables"
  exit 1
fi

echo "If the submodule update fails, please regenerate the Vercel submodule token on Github and register it as the GITHUB_PAT environment variable in Vercel."
echo "https://github.com/settings/tokens?type=beta"
echo "https://vercel.com/<my-own-projects>/<project-name>/settings/environment-variables"

git submodule set-url <my-submodule> "https://${GITHUB_PAT}@github.com/ytyng/<my-submodule>.git"

git submodule sync
git submodule update --init

# The original build script
npm run generate

4. Write the vercel.json

Since this is for generating static content with Nuxt, it will look like this:

{
  "buildCommand": "sh/build-for-vercel.sh",
  "outputDirectory": ".output/public"
}

Note

At the beginning of the build, you will inevitably see the warning:

Warning: Failed to fetch one or more git submodules

but you can ignore it.

Image

Additional Information

You can also make it work by re-cloning instead of updating the submodule.

rm -r <my-submodule>
git clone --depth 1 --branch main "https://${GITHUB_PAT}@github.com/ytyng/<my-submodule>.git" <my-submodule>
Currently unrated

Comments

Archive

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011