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
Go to the following page: https://github.com/settings/tokens?type=beta
Click on "Generate new token."
For Repository access, select "Only select repositories" and choose the minimum necessary repositories.
For Repository permissions, grant Read-only access to Contents only.
Click the "Generate token" button to create the token.
Register it in the Settings → Environment Variables section of your Vercel project. I registered it under the name GITHUB_PAT.
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
Since this is for generating static content with Nuxt, it will look like this:
{
"buildCommand": "sh/build-for-vercel.sh",
"outputDirectory": ".output/public"
}
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.
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>
Comments