Cloud Run 設定
在 Google Cloud Run 上設定 Remotion 渲染環境的完整指南,涵蓋 GCP 專案建立、服務部署及基本設定。
Cloud Run 設定
Google Cloud Run 讓你能以無伺服器方式在雲端執行 Remotion 渲染任務,無需自行管理底層基礎設施。本文說明如何從零開始完成整套設定。
實驗性功能:Cloud Run 目前處於 Alpha 狀態,並未積極開發中。
前置需求
- Google Cloud 帳號,且已啟用帳單
- 已安裝 Google Cloud CLI(gcloud)
- Node.js 18 以上版本
- 已建立的 Remotion 專案
步驟一:安裝套件
npm install @remotion/cloudrun步驟二:建立 GCP 專案
前往 Google Cloud Console 管理資源頁面,點擊「建立專案」。
接著在 Cloud Shell 或本機終端機登入並設定專案:
gcloud auth login
gcloud config set project YOUR_PROJECT_ID啟用所需的 GCP API:
gcloud services enable run.googleapis.com
gcloud services enable artifactregistry.googleapis.com
gcloud services enable storage.googleapis.com
gcloud services enable cloudbuild.googleapis.com步驟三:設定權限與服務帳戶
使用 Remotion 提供的安裝腳本,一鍵完成 IAM 設定:
curl -L https://github.com/remotion-dev/remotion/raw/main/packages/cloudrun/src/gcpInstaller/gcpInstaller.tar \
| tar -x -C . && node install.mjs依提示選擇操作:
- 選
1:為此 GCP 專案設定 Remotion Cloud Run - 選
2:產生或管理.env服務帳戶金鑰
完成後將產生的環境變數複製到本機 .env 檔案:
# 查看產生的環境變數
cat .env本機 .env 範例內容:
REMOTION_GCP_PROJECT_ID=my-remotion-project
REMOTION_GCP_CLIENT_EMAIL=remotion-sa@my-remotion-project.iam.gserviceaccount.com
REMOTION_GCP_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n..."
步驟四:驗證權限
確認 GCP 權限設定正確:
npx remotion cloudrun permissions若設定正確,所有項目應顯示為通過狀態。
步驟五:部署 Cloud Run 服務
npx remotion cloudrun services deploy指定地區與資源配置:
npx remotion cloudrun services deploy \
--region=asia-east1 \
--cpu=4 \
--memory=8Gi \
--timeout=1800也可使用 Node.js API:
import { deployService } from "@remotion/cloudrun";
const deployResult = await deployService({
projectID: "my-remotion-project",
region: "asia-east1",
});
console.log("服務名稱:", deployResult.fullName);步驟六:部署 Remotion 站台
將 Remotion 專案打包並上傳至 Cloud Storage:
npx remotion cloudrun sites create src/index.ts --site-name=my-site成功後會回傳 serveUrl,之後渲染時需要使用:
Site: my-site
Serve URL: https://storage.googleapis.com/remotion-my-project/sites/my-site/index.html
步驟七:渲染第一支影片
npx remotion cloudrun render \
https://storage.googleapis.com/my-bucket/sites/my-site/index.html \
MyComposition或使用 Node.js API:
import { renderMediaOnCloudrun } from "@remotion/cloudrun";
const result = await renderMediaOnCloudrun({
region: "asia-east1",
serviceName: "remotion-render-4-0-0",
serveUrl: "https://storage.googleapis.com/...",
composition: "MyComposition",
inputProps: { title: "我的影片" },
codec: "h264",
});
if (result.type === "success") {
console.log("輸出網址:", result.publicUrl);
}服務版本管理
Cloud Run 服務版本必須與本機 @remotion/cloudrun 套件版本一致。升級後需重新部署:
# 列出現有服務
npx remotion cloudrun services ls
# 刪除舊版本
npx remotion cloudrun services rm remotion-render-OLD_VERSION --region=asia-east1
# 部署新版本
npx remotion cloudrun services deploy --region=asia-east1常用資源設定對照表
| 影片類型 | 建議 CPU | 建議記憶體 | 建議逾時 |
|---|---|---|---|
| 短片(< 30 秒) | 2 | 4Gi | 300 秒 |
| 中片(30–120 秒) | 4 | 8Gi | 900 秒 |
| 長片(> 120 秒) | 8 | 16Gi | 3600 秒 |
小結
- 安裝
@remotion/cloudrun並使用安裝腳本設定 GCP 權限 - 使用
npx remotion cloudrun services deploy部署渲染服務 - 使用
npx remotion cloudrun sites create上傳 Remotion 專案 - 服務版本需與本機套件版本一致,升級時需重新部署