mirror of
https://github.com/beilunyang/moemail.git
synced 2025-09-26 19:41:22 +08:00
chore: Remove environment variable DATABASE_ID and KV_NAMESPACE_ID, auto-pagination kvNamespaces
This commit is contained in:
@@ -5,7 +5,6 @@ AUTH_SECRET = ""
|
||||
CLOUDFLARE_API_TOKEN = ""
|
||||
CLOUDFLARE_ACCOUNT_ID = ""
|
||||
DATABASE_NAME = ""
|
||||
DATABASE_ID = ""
|
||||
KV_NAMESPACE_ID = ""
|
||||
KV_NAMESPACE_NAME = ""
|
||||
|
||||
CUSTOM_DOMAIN = ""
|
4
.github/workflows/deploy.yml
vendored
4
.github/workflows/deploy.yml
vendored
@@ -41,10 +41,8 @@ jobs:
|
||||
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
||||
PROJECT_NAME: ${{ secrets.PROJECT_NAME }}
|
||||
DATABASE_NAME: ${{ secrets.DATABASE_NAME }}
|
||||
KV_NAME: ${{ secrets.KV_NAME }}
|
||||
KV_NAMESPACE_NAME: ${{ secrets.KV_NAMESPACE_NAME }}
|
||||
CUSTOM_DOMAIN: ${{ secrets.CUSTOM_DOMAIN }}
|
||||
DATABASE_ID: ${{ secrets.DATABASE_ID }}
|
||||
KV_NAMESPACE_ID: ${{ secrets.KV_NAMESPACE_ID }}
|
||||
AUTH_GITHUB_ID: ${{ secrets.AUTH_GITHUB_ID }}
|
||||
AUTH_GITHUB_SECRET: ${{ secrets.AUTH_GITHUB_SECRET }}
|
||||
AUTH_SECRET: ${{ secrets.AUTH_SECRET }}
|
||||
|
@@ -5,120 +5,88 @@ const CF_ACCOUNT_ID = process.env.CLOUDFLARE_ACCOUNT_ID!;
|
||||
const CF_API_TOKEN = process.env.CLOUDFLARE_API_TOKEN;
|
||||
const CUSTOM_DOMAIN = process.env.CUSTOM_DOMAIN;
|
||||
const PROJECT_NAME = process.env.PROJECT_NAME || "moemail";
|
||||
const DB_NAME = process.env.DATABASE_NAME || "moemail-db";
|
||||
const KV_NAMESPACE_NAME = process.env.KV_NAME || "moemail-kv";
|
||||
const DATABASE_NAME = process.env.DATABASE_NAME || "moemail-db";
|
||||
const KV_NAMESPACE_NAME = process.env.KV_NAMESPACE_NAME || "moemail-kv";
|
||||
|
||||
const client = new Cloudflare({
|
||||
apiKey: CF_API_TOKEN,
|
||||
});
|
||||
|
||||
export const getPages = async () => {
|
||||
try {
|
||||
const projectInfo = await client.pages.projects.get(PROJECT_NAME, {
|
||||
account_id: CF_ACCOUNT_ID,
|
||||
});
|
||||
const projectInfo = await client.pages.projects.get(PROJECT_NAME, {
|
||||
account_id: CF_ACCOUNT_ID,
|
||||
});
|
||||
|
||||
return projectInfo;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
return projectInfo;
|
||||
};
|
||||
|
||||
export const createPages = async () => {
|
||||
try {
|
||||
console.log(`🆕 Creating new Cloudflare Pages project: "${PROJECT_NAME}"`);
|
||||
console.log(`🆕 Creating new Cloudflare Pages project: "${PROJECT_NAME}"`);
|
||||
|
||||
const project = await client.pages.projects.create({
|
||||
const project = await client.pages.projects.create({
|
||||
account_id: CF_ACCOUNT_ID,
|
||||
name: PROJECT_NAME,
|
||||
production_branch: "main",
|
||||
});
|
||||
|
||||
if (CUSTOM_DOMAIN) {
|
||||
console.log("🔗 Setting pages domain...");
|
||||
|
||||
await client.pages.projects.domains.create(PROJECT_NAME, {
|
||||
account_id: CF_ACCOUNT_ID,
|
||||
name: PROJECT_NAME,
|
||||
production_branch: "main",
|
||||
name: CUSTOM_DOMAIN?.split("://")[1],
|
||||
});
|
||||
|
||||
if (CUSTOM_DOMAIN) {
|
||||
console.log("🔗 Setting pages domain...");
|
||||
|
||||
await client.pages.projects.domains.create(PROJECT_NAME, {
|
||||
account_id: CF_ACCOUNT_ID,
|
||||
name: CUSTOM_DOMAIN?.split("://")[1],
|
||||
});
|
||||
|
||||
console.log("✅ Pages domain set successfully");
|
||||
}
|
||||
|
||||
console.log("✅ Project created successfully");
|
||||
|
||||
return project;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
console.log("✅ Pages domain set successfully");
|
||||
}
|
||||
|
||||
console.log("✅ Project created successfully");
|
||||
|
||||
return project;
|
||||
};
|
||||
|
||||
export const getDatabase = async () => {
|
||||
try {
|
||||
const database = await client.d1.database.get(DB_NAME, {
|
||||
account_id: CF_ACCOUNT_ID,
|
||||
});
|
||||
const database = await client.d1.database.get(DATABASE_NAME, {
|
||||
account_id: CF_ACCOUNT_ID,
|
||||
});
|
||||
|
||||
return database;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
return database;
|
||||
};
|
||||
|
||||
export const createDatabase = async () => {
|
||||
try {
|
||||
console.log(`🆕 Creating new D1 database: "${DB_NAME}"`);
|
||||
const database = await client.d1.database.create({
|
||||
account_id: CF_ACCOUNT_ID,
|
||||
name: DB_NAME,
|
||||
});
|
||||
console.log("✅ Database created successfully");
|
||||
console.log(`🆕 Creating new D1 database: "${DATABASE_NAME}"`);
|
||||
|
||||
return database;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
const database = await client.d1.database.create({
|
||||
account_id: CF_ACCOUNT_ID,
|
||||
name: DATABASE_NAME,
|
||||
});
|
||||
|
||||
export const getKVNamespace = async (namespaceId: string) => {
|
||||
if (!namespaceId) {
|
||||
throw new Error("KV namespace ID is required");
|
||||
}
|
||||
|
||||
try {
|
||||
const kvNamespace = await client.kv.namespaces.get(namespaceId, {
|
||||
account_id: CF_ACCOUNT_ID,
|
||||
});
|
||||
console.log("✅ Database created successfully");
|
||||
|
||||
return kvNamespace;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
return database;
|
||||
};
|
||||
|
||||
export const getKVNamespaceList = async () => {
|
||||
try {
|
||||
const kvNamespaces = await client.kv.namespaces.list({
|
||||
account_id: CF_ACCOUNT_ID,
|
||||
});
|
||||
const kvNamespaces = [];
|
||||
|
||||
return kvNamespaces;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
for await (const namespace of client.kv.namespaces.list({
|
||||
account_id: CF_ACCOUNT_ID,
|
||||
})) {
|
||||
kvNamespaces.push(namespace);
|
||||
}
|
||||
}
|
||||
|
||||
return kvNamespaces;
|
||||
};
|
||||
|
||||
export const createKVNamespace = async () => {
|
||||
try {
|
||||
console.log(`🆕 Creating new KV namespace: "${KV_NAMESPACE_NAME}"`);
|
||||
const kvNamespace = await client.kv.namespaces.create({
|
||||
account_id: CF_ACCOUNT_ID,
|
||||
title: KV_NAMESPACE_NAME,
|
||||
});
|
||||
console.log("✅ KV namespace created successfully");
|
||||
console.log(`🆕 Creating new KV namespace: "${KV_NAMESPACE_NAME}"`);
|
||||
|
||||
return kvNamespace;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
const kvNamespace = await client.kv.namespaces.create({
|
||||
account_id: CF_ACCOUNT_ID,
|
||||
title: KV_NAMESPACE_NAME,
|
||||
});
|
||||
|
||||
console.log("✅ KV namespace created successfully");
|
||||
|
||||
return kvNamespace;
|
||||
};
|
||||
|
@@ -8,17 +8,14 @@ import {
|
||||
createKVNamespace,
|
||||
createPages,
|
||||
getDatabase,
|
||||
getKVNamespace,
|
||||
getKVNamespaceList,
|
||||
getPages,
|
||||
} from "./cloudflare";
|
||||
|
||||
const PROJECT_NAME = process.env.PROJECT_NAME || "moemail";
|
||||
const DATABASE_NAME = process.env.DATABASE_NAME || "moemail-db";
|
||||
const KV_NAMESPACE_NAME = process.env.KV_NAME || "moemail-kv";
|
||||
const KV_NAMESPACE_NAME = process.env.KV_NAMESPACE_NAME || "moemail-kv";
|
||||
const CUSTOM_DOMAIN = process.env.CUSTOM_DOMAIN;
|
||||
const DATABASE_ID = process.env.DATABASE_ID || "";
|
||||
const KV_NAMESPACE_ID = process.env.KV_NAMESPACE_ID || "";
|
||||
|
||||
/**
|
||||
* 验证必要的环境变量
|
||||
@@ -56,14 +53,6 @@ const setupConfigFile = (examplePath: string, targetPath: string) => {
|
||||
// 处理数据库配置
|
||||
if (json.d1_databases && json.d1_databases.length > 0) {
|
||||
json.d1_databases[0].database_name = DATABASE_NAME;
|
||||
if (DATABASE_ID) {
|
||||
json.d1_databases[0].database_id = DATABASE_ID;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理KV配置
|
||||
if (json.kv_namespaces && json.kv_namespaces.length > 0 && KV_NAMESPACE_ID) {
|
||||
json.kv_namespaces[0].id = KV_NAMESPACE_ID;
|
||||
}
|
||||
|
||||
// 写入配置文件
|
||||
@@ -102,9 +91,6 @@ const setupWranglerConfigs = () => {
|
||||
const updateDatabaseConfig = (dbId: string) => {
|
||||
console.log(`📝 Updating database ID (${dbId}) in configurations...`);
|
||||
|
||||
// 更新环境变量
|
||||
updateEnvVar("DATABASE_ID", dbId);
|
||||
|
||||
// 更新所有配置文件
|
||||
const configFiles = [
|
||||
"wrangler.json",
|
||||
@@ -135,9 +121,6 @@ const updateDatabaseConfig = (dbId: string) => {
|
||||
const updateKVConfig = (namespaceId: string) => {
|
||||
console.log(`📝 Updating KV namespace ID (${namespaceId}) in configurations...`);
|
||||
|
||||
// 更新环境变量
|
||||
updateEnvVar("KV_NAMESPACE_ID", namespaceId);
|
||||
|
||||
// KV命名空间只在主wrangler.json中使用
|
||||
const wranglerPath = resolve("wrangler.json");
|
||||
if (existsSync(wranglerPath)) {
|
||||
@@ -215,24 +198,17 @@ const checkAndCreateKVNamespace = async () => {
|
||||
try {
|
||||
let namespace;
|
||||
|
||||
if (KV_NAMESPACE_ID) {
|
||||
namespace = await getKVNamespace(KV_NAMESPACE_ID);
|
||||
console.log(`✅ KV namespace "${KV_NAMESPACE_NAME}" already exists (ID: ${namespace.id})`);
|
||||
const namespaceList = await getKVNamespaceList();
|
||||
namespace = namespaceList.find(ns => ns.title === KV_NAMESPACE_NAME);
|
||||
|
||||
if (namespace && namespace.id) {
|
||||
updateKVConfig(namespace.id);
|
||||
console.log(`✅ KV namespace "${KV_NAMESPACE_NAME}" found by name (ID: ${namespace.id})`);
|
||||
} else {
|
||||
console.log("⚠️ KV_NAMESPACE_ID is not set, checking by name...");
|
||||
|
||||
const namespaceList = await getKVNamespaceList();
|
||||
namespace = namespaceList.result.find(ns => ns.title === KV_NAMESPACE_NAME);
|
||||
|
||||
if (namespace && namespace.id) {
|
||||
updateKVConfig(namespace.id);
|
||||
console.log(`✅ KV namespace "${KV_NAMESPACE_NAME}" found by name (ID: ${namespace.id})`);
|
||||
} else {
|
||||
console.log("⚠️ KV namespace not found by name, creating new KV namespace...");
|
||||
namespace = await createKVNamespace();
|
||||
updateKVConfig(namespace.id);
|
||||
console.log(`✅ KV namespace "${KV_NAMESPACE_NAME}" created successfully (ID: ${namespace.id})`);
|
||||
}
|
||||
console.log("⚠️ KV namespace not found by name, creating new KV namespace...");
|
||||
namespace = await createKVNamespace();
|
||||
updateKVConfig(namespace.id);
|
||||
console.log(`✅ KV namespace "${KV_NAMESPACE_NAME}" created successfully (ID: ${namespace.id})`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`❌ An error occurred while checking the KV namespace:`, error);
|
||||
@@ -433,6 +409,7 @@ const updateEnvVar = (name: string, value: string) => {
|
||||
const main = async () => {
|
||||
try {
|
||||
console.log("🚀 Starting deployment process...");
|
||||
|
||||
validateEnvironment();
|
||||
setupEnvFile();
|
||||
setupWranglerConfigs();
|
||||
|
Reference in New Issue
Block a user