forked from cloudflare/vibesdk-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy_templates.sh
More file actions
executable file
·222 lines (187 loc) · 6.79 KB
/
deploy_templates.sh
File metadata and controls
executable file
·222 lines (187 loc) · 6.79 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/bin/bash
set -euo pipefail
echo "🚀 Starting template deployment process..."
# Ensure PyYAML is installed
echo "🐍 Checking Python dependencies..."
if ! python3 -c "import yaml" 2>/dev/null; then
echo "📦 Installing PyYAML..."
pip3 install pyyaml || pip install pyyaml || {
echo "❌ Failed to install PyYAML. Please install it manually: pip install pyyaml"
exit 1
}
echo "✅ PyYAML installed successfully"
else
echo "✅ PyYAML is already installed"
fi
# 1) Generate templates into build/
echo "🧱 Generating templates into build/..."
python3 tools/generate_templates.py --clean
echo "✅ Templates generated"
# 2) Generate template catalog (generate_template_catalog.py now defaults to ./build)
echo "📋 Generating template catalog..."
python3 generate_template_catalog.py --output template_catalog.json --pretty
echo "✅ Generated template catalog"
# Create optimized zip files for templates
echo "📦 Creating optimized zip files for templates..."
# Create zips directory
mkdir -p zips
# Function to create fast-extracting zip files using Python
create_template_zip() {
local template_dir="$1"
local template_name=$(basename "$template_dir")
local zip_file="zips/${template_name}.zip"
echo "Creating zip for: $template_name"
# Use Python script to create zip (compatible with environments without zip command)
if python3 create_zip.py "$template_dir" "$zip_file"; then
# Verify the zip file was created
if [ -f "$zip_file" ]; then
local size=$(du -h "$zip_file" | cut -f1)
echo "✅ Created $zip_file ($size)"
else
echo "❌ Failed to create $zip_file"
return 1
fi
else
echo "❌ Failed to create $zip_file using Python"
return 1
fi
}
# 3) Create zip for each valid template directory in build/ in parallel
pids=()
for dir in build/*/; do
# Skip non-directories and hidden directories
if [[ ! -d "$dir" || "$dir" == .* ]]; then
continue
fi
dir_name=$(basename "$dir")
# Skip non-template directories if any appear
if [[ "$dir_name" == ".git" || "$dir_name" == "node_modules" || "$dir_name" == ".github" ]]; then
continue
fi
# Check if it's a valid template (has required files)
if [[ -f "$dir/package.json" && (-f "$dir/wrangler.jsonc" || -f "$dir/wrangler.toml") && -d "$dir/prompts" ]]; then
create_template_zip "$dir" &
pids+=($!)
else
echo "⏭️ Skipping $dir_name (not a valid template)"
fi
done
# Wait for all zip creation processes to complete
echo "⏳ Waiting for all zip creation processes to complete..."
for pid in "${pids[@]}"; do
wait "$pid"
done
echo "📦 All template zips created successfully"
ls -la zips/
# Verify Wrangler CLI is available
echo "⚙️ Verifying Wrangler CLI..."
wrangler --version
echo "✅ Wrangler CLI ready"
# Determine R2 endpoint based on LOCAL_R2 environment variable
if [ "${LOCAL_R2:-}" = "true" ]; then
echo "🏠 LOCAL_R2=true - using local R2 endpoint"
R2_FLAGS="--local"
R2_ENDPOINT="local R2"
else
echo "☁️ Using remote Cloudflare R2"
R2_FLAGS="--remote"
R2_ENDPOINT="Cloudflare R2"
fi
# Upload files to R2
echo "🚀 Uploading files to $R2_ENDPOINT..."
# Function to upload a file to R2
upload_to_r2() {
local file_path="$1"
local r2_key="$2"
local description="$3"
echo "Uploading: $description"
if wrangler r2 object put "${R2_BUCKET_NAME}/$r2_key" --file="$file_path" $R2_FLAGS; then
echo "✅ Successfully uploaded $description"
return 0
else
echo "❌ Failed to upload $description"
return 1
fi
}
# Upload template catalog JSON and all zip files
if [ "${LOCAL_R2:-}" = "true" ]; then
echo "📄📦 Uploading template catalog and zip files sequentially (local R2)..."
failed_uploads=()
# Upload catalog first
if ! upload_to_r2 "template_catalog.json" "template_catalog.json" "template_catalog.json"; then
failed_uploads+=("template_catalog.json upload failed")
fi
# Upload zip files sequentially
for zip_file in zips/*.zip; do
if [ -f "$zip_file" ]; then
filename=$(basename "$zip_file")
if ! upload_to_r2 "$zip_file" "$filename" "$filename"; then
failed_uploads+=("$filename upload failed")
fi
fi
done
else
echo "📄📦 Uploading template catalog and zip files in parallel..."
upload_pids=()
failed_uploads=()
# Start catalog upload in background
upload_to_r2 "template_catalog.json" "template_catalog.json" "template_catalog.json" &
upload_pids+=($!)
# Start zip file uploads in background
for zip_file in zips/*.zip; do
if [ -f "$zip_file" ]; then
filename=$(basename "$zip_file")
upload_to_r2 "$zip_file" "$filename" "$filename" &
upload_pids+=($!)
fi
done
# Wait for all uploads to complete and check for failures
echo "⏳ Waiting for all uploads to complete..."
for pid in "${upload_pids[@]}"; do
if ! wait "$pid"; then
failed_uploads+=("Upload process $pid failed")
fi
done
fi
# Check if any uploads failed
if [ ${#failed_uploads[@]} -gt 0 ]; then
echo "❌ Some uploads failed:"
for failure in "${failed_uploads[@]}"; do
echo " - $failure"
done
exit 1
fi
echo "🎉 All files uploaded successfully to $R2_ENDPOINT bucket: ${R2_BUCKET_NAME}"
# Skip verification as wrangler doesn't have a list command
echo "✅ All uploads completed successfully"
# Generate deployment summary for GitHub Actions
if [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then
echo "## 🚀 Deployment Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### 📋 Template Catalog" >> $GITHUB_STEP_SUMMARY
echo "- ✅ Generated and uploaded \`template_catalog.json\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### 📦 Template Archives" >> $GITHUB_STEP_SUMMARY
# Count and list zip files
zip_count=$(ls zips/*.zip 2>/dev/null | wc -l)
echo "- ✅ Created and uploaded $zip_count template zip files:" >> $GITHUB_STEP_SUMMARY
for zip_file in zips/*.zip; do
if [ -f "$zip_file" ]; then
filename=$(basename "$zip_file" .zip)
size=$(du -h "$zip_file" | cut -f1)
echo " - \`$filename\` ($size)" >> $GITHUB_STEP_SUMMARY
fi
done
echo "" >> $GITHUB_STEP_SUMMARY
echo "### 🌐 Access URLs" >> $GITHUB_STEP_SUMMARY
if [ "${LOCAL_R2:-}" = "true" ]; then
echo "- **Target**: Local R2 (${R2_BUCKET_NAME})" >> $GITHUB_STEP_SUMMARY
echo "- **Note**: Files uploaded to local R2 development environment" >> $GITHUB_STEP_SUMMARY
else
echo "- **Catalog**: \`https://${R2_BUCKET_NAME}.r2.dev/template_catalog.json\`" >> $GITHUB_STEP_SUMMARY
echo "- **Templates**: \`https://${R2_BUCKET_NAME}.r2.dev/[template-name].zip\`" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "🕐 **Deployed at**: $(date -u +%Y-%m-%d\ %H:%M:%S\ UTC)" >> $GITHUB_STEP_SUMMARY
fi
echo "🎯 Template deployment completed successfully!"