-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·159 lines (136 loc) · 6.24 KB
/
deploy.sh
File metadata and controls
executable file
·159 lines (136 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env bash
config_file="wrangler.jsonc"
deploy_mode="workers"
pages_output_dir=""
# 显示帮助信息
show_help() {
echo "用法: $0 [选项]"
echo ""
echo "部署 Cloudflare Workers/Pages 应用"
echo ""
echo "选项:"
echo " -c <文件> 指定配置文件 (默认: wrangler.jsonc)"
echo " -m <模式> 部署模式: workers 或 pages (默认: workers)"
echo " -h 显示此帮助信息"
echo ""
echo "示例:"
echo " $0 # 默认使用 wrangler.jsonc 进行 Workers 部署"
echo " $0 -m pages # 使用 wrangler.jsonc 进行 Pages 部署"
echo " $0 -c custom.jsonc # 使用自定义配置文件进行 Workers 部署"
echo " $0 -c custom.jsonc -m pages # 使用自定义配置文件进行 Pages 部署"
echo ""
echo "功能:"
echo " - 自动检查并创建 DOCKER_ROUTES KV namespace"
echo " - 自动更新配置文件中的 KV namespace ID"
echo " - 支持 Workers 和 Pages 两种部署模式"
echo " - 正确处理 .jsonc 配置文件,保留所有注释"
}
# 解析参数
while getopts "c:m:h" opt; do
case $opt in
c) config_file="$OPTARG" ;;
m) deploy_mode="$OPTARG" ;;
h) show_help; exit 0 ;;
*) show_help; exit 1 ;;
esac
done
if [[ ! -f "$config_file" ]]; then
echo "错误: $config_file 未找到!"
exit 1
fi
if [[ "$deploy_mode" != "workers" && "$deploy_mode" != "pages" ]]; then
echo "错误: 部署模式必须是 'workers' 或 'pages'"
exit 1
fi
# 检查并同步 DOCKER_ROUTES KV namespace
if [[ "$config_file" == "wrangler.jsonc" ]]; then
echo "使用默认配置文件 wrangler.jsonc,跳过 KV ID 同步。"
else
echo "正在同步 DOCKER_ROUTES KV namespace..."
# 1. 从云端获取 ID
# 注意: wrangler kv namespace list 默认输出 JSON
# 增加 -c 参数避免 wrangler 因为当前配置文件的问题而拒绝执行
namespace_id=$(npx wrangler kv namespace list -c "$config_file" 2>/dev/null | jq -r '.[] | select(.title == "DOCKER_ROUTES") | .id' 2>/dev/null)
if [[ -z "$namespace_id" ]]; then
echo "云端未找到 DOCKER_ROUTES,正在尝试创建..."
# 创建 KV namespace,不再使用 --update-config 以免产生重复绑定
if npx wrangler kv namespace create DOCKER_ROUTES --binding DOCKER_ROUTES -c "$config_file" &>/dev/null; then
echo "成功创建 KV namespace"
# 获取新创建的 ID
namespace_id=$(npx wrangler kv namespace list -c "$config_file" 2>/dev/null | jq -r '.[] | select(.title == "DOCKER_ROUTES") | .id' 2>/dev/null)
else
# 再次尝试获取,可能已经存在
namespace_id=$(npx wrangler kv namespace list -c "$config_file" 2>/dev/null | jq -r '.[] | select(.title == "DOCKER_ROUTES") | .id' 2>/dev/null)
if [[ -z "$namespace_id" ]]; then
echo "错误: 无法创建或获取 DOCKER_ROUTES KV namespace,请检查 wrangler 登录状态。"
exit 1
fi
fi
fi
echo "云端 DOCKER_ROUTES ID: $namespace_id"
# 2. 同步配置文件:清空现有的 kv_namespaces 数组并重新添加,以避免重复绑定
echo "正在同步配置文件 $config_file..."
if grep -q '"kv_namespaces"[[:space:]]*:[[:space:]]*\[' "$config_file"; then
# 第一步:清空 kv_namespaces 数组内的所有内容
# 逻辑:匹配从 [ 到 ] 之间的行并删除(保留 [ 和 ] 本身)
sed -i '/"kv_namespaces"[[:space:]]*:[[:space:]]*\[/,/\]/ { /"kv_namespaces"[[:space:]]*:[[:space:]]*\[/b; /\]/b; d }' "$config_file"
# 第二步:在 [ 之后插入新的 DOCKER_ROUTES 绑定
sed -i '/"kv_namespaces"[[:space:]]*:[[:space:]]*\[/a \ \ \ \ \ \ \ \ {\n\ \ \ \ \ \ \ \ \ \ \ \ "binding": "DOCKER_ROUTES",\n\ \ \ \ \ \ \ \ \ \ \ \ "id": "'"$namespace_id"'"\n\ \ \ \ \ \ \ \ }' "$config_file"
echo "配置文件同步完成"
else
echo "警告: 配置文件中不存在 kv_namespaces 数组,无法自动同步。请手动配置。"
exit 1
fi
fi
# 根据部署模式执行相应的部署命令
echo "开始部署 (模式: $deploy_mode)..."
if [[ "$deploy_mode" == "workers" ]]; then
npx wrangler deploy -c "$config_file"
else
# Pages 模式: 需要临时调整配置文件以适应 Pages 部署
echo "正在为 Pages 模式准备配置..."
target_config="wrangler.jsonc"
backup_needed=false
# 如果指定的配置文件不是 wrangler.jsonc,由于 pages deploy 不支持 -c,需要替换 wrangler.jsonc
if [[ "$config_file" != "$target_config" ]]; then
if [[ -f "$target_config" ]]; then
echo "备份现有的 $target_config 为 $target_config.bak"
cp "$target_config" "$target_config.bak"
backup_needed=true
fi
echo "将 $config_file 复制为 $target_config 用于部署"
cp "$config_file" "$target_config"
fi
# 1. 取消 pages_build_output_dir 的注释 (移除 // )
sed -i 's|^\([[:space:]]*\)//[[:space:]]*\("pages_build_output_dir":\)|\1\2|' "$target_config"
# 2. 注释掉 Worker 的 "main": (在行首添加 // )
sed -i 's|^\([[:space:]]*\)\("main":\)|\1// \2|' "$target_config"
# 获取 pages_build_output_dir。由于 JSONC 格式可能导致 jq 报错,此处直接使用 grep 提取
pages_output_dir=$(grep -o '"pages_build_output_dir"[[:space:]]*:[[:space:]]*"[^"]*"' "$target_config" | head -1 | cut -d'"' -f4)
if [[ -z "$pages_output_dir" ]]; then
pages_output_dir="pages" # 默认目录
fi
if [[ ! -d "$pages_output_dir" ]]; then
echo "错误: Pages 输出目录 '$pages_output_dir' 不存在!"
# 尝试恢复逻辑
if [[ "$backup_needed" == true ]]; then
mv "$target_config.bak" "$target_config"
fi
exit 1
fi
# 执行部署 (Wrangler 会自动从已取消注释的 wrangler.jsonc 中读取配置)
npx wrangler pages deploy
# 部署结束后,恢复至原来的状态
echo "部署结束,正在恢复配置..."
if [[ "$backup_needed" == true ]]; then
echo "从备份恢复 $target_config"
mv "$target_config.bak" "$target_config"
else
# 如果原本就是修改的 wrangler.jsonc,则通过 sed 恢复
# 1. 恢复注释 pages_build_output_dir
sed -i 's|^\([[:space:]]*\)\("pages_build_output_dir":\)|\1// \2|' "$target_config"
# 2. 恢复取消注释 main
sed -i 's|^\([[:space:]]*\)//[[:space:]]*\("main":\)|\1\2|' "$target_config"
fi
fi
echo "部署完成"