プライベートな Git サブモジュールがあるプロジェクトを Vercel にデプロイする方法

2024-05-27 00:38 (2ヶ月前) ytyng
View in English

Vercel のビルド時は、プライベートリポジトリとなっているサブモジュールを取得できない。

Github上の Vercel のアプリに権限を付与してもできない。

Fine-granted personal access token を使って HTTP で取得させる必要がある。

キモとしては、ビルド中に下記を行う。

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

1. Fine-granted personal access token を作る

https://github.com/settings/tokens?type=beta

上記ページから、 Generate new token をクリック。

画像

Repository access は、 Only select repositories を選択し、必要最低限のリポジトリを選択する。

Repository permissions は Contents の Read-only のみ付与する。

画像

Generate token ボタンを押して生成する。

2. Vercel の環境変数に登録する

Vercel のプロジェクトの Settings → Environment Variables に登録する。私は GITHUB_PAT という名前で登録した。

画像

3. ビルドスクリプトを作る

sh/build-for-vercel.sh という名前で作った。

#!/usr/bin/env bash

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

if [ -z "${GITHUB_PAT}" ]; then
  echo "環境変数 GITHUB_PAT が設定されていません。Github の vercel-submodule トークンを再生成して、Vercel の環境変数 GITHUB_PAT に登録してください。"
  echo "https://github.com/settings/tokens?type=beta"
  echo "https://vercel.com/<my-own-projects>/<project-name>/settings/environment-variables"
  exit 1
fi

echo "サブモジュールの更新に失敗する場合は、Github の vercel-submodule トークンを再生成して、Vercel の環境変数 GITHUB_PAT に登録してください。"
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

# 本来のビルドスクリプト
npm run generate

4. vercel.json を書く

今回は Nuxt の静的コンテンツ生成なのでこのような形になる

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

注意

ビルドの最初で、どうしても

Warning: Failed to fetch one or more git submodules

の警告が出てしまうが、気にしない。

画像

補足

submodule update ではなくクローンしなおしても動く。

rm -r <my-submodule>
git clone --depth 1 --branch main "https://${GITHUB_PAT}@github.com/ytyng/<my-submodule>.git" <my-submodule>
現在未評価
タイトルとURLをコピー

コメント

アーカイブ

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