-
Notifications
You must be signed in to change notification settings - Fork 6
432 lines (363 loc) · 16.8 KB
/
deploy-docs.yml
File metadata and controls
432 lines (363 loc) · 16.8 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
name: Deploy Documentation
on:
workflow_dispatch:
inputs:
subfolder:
description: 'Subfolder for documentation (leave empty for root deployment)'
required: false
default: ''
type: string
triggering_pr_number:
description: 'PR number that triggered this deployment'
required: false
default: ''
type: string
triggering_pr_title:
description: 'PR title that triggered this deployment'
required: false
default: ''
type: string
triggering_commit_sha:
description: 'Commit SHA that triggered this deployment'
required: false
default: ''
type: string
triggering_branch:
description: 'Branch that triggered this deployment'
required: false
default: ''
type: string
original_pr_number:
description: 'Original PR number (if different from triggering PR)'
required: false
default: ''
type: string
permissions:
contents: write
pull-requests: write
# Ensure only latest deployment runs
concurrency:
group: docs-deployment
cancel-in-progress: true
jobs:
merge-docs:
runs-on: ubuntu-latest
steps:
- name: Generate GitHub App token
id: app-token
uses: actions/create-github-app-token@d72941d797fd3113feb6b93fd0dec494b13a2547 # v1
with:
app-id: ${{ secrets.PROBE_APP_ID }}
private-key: ${{ secrets.PROBE_APP_PRIVATE_KEY }}
owner: TykTechnologies
- name: Log deployment trigger information
run: |
echo "🚀 Starting documentation deployment"
echo "📋 Trigger Information:"
echo " - Branch: ${{ inputs.triggering_branch || 'N/A' }}"
echo " - Commit: ${{ inputs.triggering_commit_sha || 'N/A' }}"
if [ -n "${{ inputs.triggering_pr_number }}" ]; then
echo " - PR: #${{ inputs.triggering_pr_number }} - ${{ inputs.triggering_pr_title }}"
if [ -n "${{ inputs.original_pr_number }}" ] && [ "${{ inputs.original_pr_number }}" != "${{ inputs.triggering_pr_number }}" ]; then
echo " - Original PR: #${{ inputs.original_pr_number }} (cherry-picked)"
fi
else
echo " - PR: N/A (direct push)"
fi
echo " - Timestamp: $(date)"
echo " - Workflow Run: ${{ github.run_id }}"
echo " - Subfolder: ${{ inputs.subfolder || '(root deployment)' }}"
- name: Checkout production branch
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
with:
ref: production
token: ${{ steps.app-token.outputs.token }}
fetch-depth: 0 # Fetch all history for all branches
- name: Set up Python
uses: actions/setup-python@7f4fc3e22c37d6ff65e88745f38bd3157c663f7c # v4
with:
python-version: '3.9'
- name: Install dependencies
run: |
python -m pip install --upgrade 'pip>=23.0,<25'
# Add any additional dependencies if needed
- name: Read branches config
id: config
run: |
if [ ! -f "branches-config.json" ]; then
echo "❌ branches-config.json not found!"
exit 1
fi
echo "✅ Found branches-config.json"
cat branches-config.json
- name: Cleanup old version folders and assets
run: |
echo "🧹 Cleaning up all content except essential files..."
# Define folders to keep (whitelist)
keep_folders=(
".github"
".git"
".devcontainer"
"scripts"
".vale"
".probe"
)
# Define files to keep (whitelist)
keep_files=(
"branches-config.json"
".gitignore"
"README.md"
".vale.ini"
)
echo "📋 Folders to keep: ${keep_folders[*]}"
echo "📋 Files to keep: ${keep_files[*]}"
# Remove all directories except the ones we want to keep
echo "🗑️ Removing old directories..."
for dir in */; do
if [ -d "$dir" ]; then
dir_name=${dir%/} # Remove trailing slash
should_keep=false
for keep_folder in "${keep_folders[@]}"; do
if [ "$dir_name" = "$keep_folder" ]; then
should_keep=true
break
fi
done
if [ "$should_keep" = false ]; then
echo "🗑️ Removing directory: $dir_name/"
rm -rf "$dir"
else
echo "📁 Keeping essential directory: $dir_name/"
fi
fi
done
# Remove all files except the ones we want to keep
echo "🗑️ Removing old files..."
for file in *; do
if [ -f "$file" ]; then
should_keep=false
for keep_file in "${keep_files[@]}"; do
if [ "$file" = "$keep_file" ]; then
should_keep=true
break
fi
done
if [ "$should_keep" = false ]; then
echo "🗑️ Removing file: $file"
rm -f "$file"
else
echo "📄 Keeping essential file: $file"
fi
fi
done
echo "✅ Cleanup completed!"
echo "📁 Remaining files after cleanup:"
ls -la | grep -E "^[d-]" | head -10
- name: Clone and organize branches
run: |
set -e
echo "🔄 Starting branch cloning and organization..."
# Read the branches config and extract branch information
branches=$(python3 -c "import json; config=json.load(open('branches-config.json')); [print(f\"{v.get('sourceFolder','')}:{v.get('branch','main')}\") for v in config.get('versions',[]) if v.get('sourceFolder','')]")
echo "📋 Branches to process:"
echo "$branches"
# Clone each branch into its respective folder
for branch_info in $branches; do
if [ -n "$branch_info" ]; then
folder=$(echo $branch_info | cut -d':' -f1)
branch=$(echo $branch_info | cut -d':' -f2)
echo "📂 Processing $folder from branch $branch..."
# Remove existing folder if it exists
if [ -d "$folder" ]; then
echo "🗑️ Removing existing $folder/"
rm -rf "$folder"
fi
# Create temporary directory for this branch
temp_dir="temp_$folder"
# Clone the specific branch
echo "⬇️ Cloning branch $branch into $temp_dir..."
git clone --single-branch --branch "$branch" --depth 1 \
"https://github.com/${{ github.repository }}.git" "$temp_dir"
# Move the contents (excluding .git) to the target folder
mkdir -p "$folder"
echo "📁 Moving contents from $temp_dir to $folder..."
# Copy documentation content only, excluding development/build files
# Excluded: .git, scripts/, branches-config.json, .github/, README.md, .gitignore, .devcontainer/, .probe
find "$temp_dir" -mindepth 1 -maxdepth 1 \
! -name '.git' \
! -name 'scripts' \
! -name 'branches-config.json' \
! -name '.github' \
! -name 'README.md' \
! -name '.gitignore' \
! -name '.devcontainer' \
! -name '.probe' \
-exec cp -r {} "$folder/" \;
# Clean up temp directory
rm -rf "$temp_dir"
echo "✅ Successfully organized $folder from branch $branch"
# Show what was copied
echo "📋 Contents of $folder:"
ls -la "$folder/" | head -10
fi
done
echo "🎉 All branches cloned and organized!"
- name: Run docs merger
run: |
echo "🔄 Running documentation merger..."
# Get subfolder from input (no fallback - empty means root deployment)
SUBFOLDER="${{ inputs.subfolder }}"
echo "📁 Using subfolder: '$SUBFOLDER'"
# Run the merge script with branches config
if [ -n "$SUBFOLDER" ]; then
echo "📁 Deploying to subfolder: $SUBFOLDER"
python3 scripts/merge_docs_configs.py \
--branches-config branches-config.json \
--base-dir . \
--subfolder "$SUBFOLDER" \
--output docs.json
else
echo "📁 Deploying to root (no subfolder)"
python3 scripts/merge_docs_configs.py \
--branches-config branches-config.json \
--base-dir . \
--output docs.json
fi
echo "✅ Documentation merge completed!"
- name: Cleanup cloned version folders
run: |
echo "🧹 Cleaning up temporary cloned version folders..."
# Get current version folders from config
current_folders=$(python3 -c "import json; config=json.load(open('branches-config.json')); folders=[v.get('sourceFolder','') for v in config.get('versions',[]) if v.get('sourceFolder','')]; print(' '.join(folders))")
echo "📋 Removing cloned folders: $current_folders"
# Remove the temporary cloned folders (they've been processed into subfolder structure)
for folder in $current_folders; do
if [ -n "$folder" ] && [ -d "$folder" ]; then
echo "🗑️ Removing cloned folder: $folder/"
rm -rf "$folder"
fi
done
echo "✅ Cleanup of cloned folders completed!"
- name: Add canonical URLs to MDX files
run: |
echo "🧩 Running canonical URL update script..."
python3 scripts/add-canonical-urls/index.py || { echo "❌ Canonical script failed"; exit 1; }
echo "✅ Canonical URL update completed."
- name: Verify output
run: |
echo "📋 Checking generated files..."
if [ -f "docs.json" ]; then
echo "✅ docs.json generated successfully"
echo "📄 docs.json size: $(wc -c < docs.json) bytes"
echo "🔍 First few lines of docs.json:"
head -20 docs.json
else
echo "❌ docs.json not found!"
exit 1
fi
echo ""
echo "📁 Generated file structure:"
find . -name "*.mdx" -o -name "*.md" -o -name "*.json" -o -name "*.css" -o -name "*.png" | head -20
- name: Close previous deployment PRs
run: |
echo "🔍 Finding and closing previous deployment PRs..."
# Find PRs that match BOTH criteria:
# 1. Have the "auto-deployment" label
# 2. Branch starts with "docs-merge-"
DEPLOYMENT_PRS=$(gh pr list \
--state open \
--label "auto-deployment" \
--json number,headRefName,labels \
--jq '.[] | select(.headRefName | startswith("docs-merge-")) | .number')
if [ -n "$DEPLOYMENT_PRS" ]; then
echo "📋 Found deployment PRs to close: $DEPLOYMENT_PRS"
for pr_number in $DEPLOYMENT_PRS; do
echo "❌ Closing deployment PR #$pr_number"
gh pr close "$pr_number" \
--comment "🤖 Superseded by newer deployment (Run #${{ github.run_number }})" \
|| echo "⚠️ Failed to close PR #$pr_number (may already be closed)"
done
echo "✅ Closed all previous deployment PRs"
else
echo "✅ No deployment PRs found to close"
fi
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
- name: Create Pull Request
id: create-pr
uses: peter-evans/create-pull-request@22a9089034f40e5a961c8808d113e2c98fb63676 # v7
with:
token: ${{ steps.app-token.outputs.token }}
branch: docs-merge-${{ github.run_number }}
base: production
title: "🤖 Auto-merge documentation from branches"
body: |
## 📚 Documentation Merge Summary
This PR contains automatically merged documentation from multiple branches.
**Triggered by:**
- **Branch:** ${{ inputs.triggering_branch || 'N/A' }}
- **Commit:** ${{ inputs.triggering_commit_sha || 'N/A' }}
- **PR:** ${{ inputs.triggering_pr_number && format('#{0} - {1}', inputs.triggering_pr_number, inputs.triggering_pr_title) || 'N/A (direct push)' }}${{ inputs.original_pr_number && inputs.original_pr_number != inputs.triggering_pr_number && format(' (cherry-pick of #{0})', inputs.original_pr_number) || '' }}
**Deployment Details:**
- **Generated from:** `branches-config.json`
- **Run ID:** ${{ github.run_id }}
- **Subfolder:** `${{ inputs.subfolder || '(root deployment)' }}`
- **Timestamp:** $(date)
### Changes Include:
- ✅ Merged documentation from multiple branches
- ✅ Generated unified `docs.json` configuration
- ✅ Updated assets and content structure
- ✅ Cleaned up outdated version folders
---
🚦 **This PR will be processed by merge queue to ensure proper validation and ordering.**
Previous deployment PRs have been automatically closed to prevent conflicts.
labels: |
documentation
auto-deployment
automated
commit-message: |
🤖 Auto-merge documentation from branches
Generated from branches-config.json at ${{ github.event.head_commit.timestamp }}
- Merged documentation from multiple branches
- Generated unified docs.json
- Updated assets and content structure
delete-branch: true
draft: false
maintainer-can-modify: true
author: ${{ github.actor }} <${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com>
committer: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- name: Enable auto-merge on deployment PR
if: steps.create-pr.outputs.pull-request-number
run: |
PR_NUMBER="${{ steps.create-pr.outputs.pull-request-number }}"
echo "� Enabling auto-merge on deployment PR #$PR_NUMBER..."
gh pr merge --squash --auto "$PR_NUMBER"
echo "✅ Auto-merge enabled on PR #$PR_NUMBER"
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
- name: Create deployment summary
run: |
echo "## 📚 Documentation Deployment Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### 🚀 Trigger Information" >> $GITHUB_STEP_SUMMARY
echo "- **Branch:** ${{ inputs.triggering_branch || 'N/A' }}" >> $GITHUB_STEP_SUMMARY
echo "- **Commit:** ${{ inputs.triggering_commit_sha || 'N/A' }}" >> $GITHUB_STEP_SUMMARY
if [ -n "${{ inputs.triggering_pr_number }}" ]; then
echo "- **PR:** #${{ inputs.triggering_pr_number }} - ${{ inputs.triggering_pr_title }}" >> $GITHUB_STEP_SUMMARY
if [ -n "${{ inputs.original_pr_number }}" ] && [ "${{ inputs.original_pr_number }}" != "${{ inputs.triggering_pr_number }}" ]; then
echo "- **Original PR:** #${{ inputs.original_pr_number }} (cherry-picked)" >> $GITHUB_STEP_SUMMARY
fi
else
echo "- **PR:** N/A (direct push)" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "### ✅ Successfully merged documentation" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Generated files:**" >> $GITHUB_STEP_SUMMARY
echo "- \`docs.json\` - Unified configuration" >> $GITHUB_STEP_SUMMARY
if [ -f "docs.json" ]; then
echo "- Asset files (favicon.png, style.css, images/, etc.)" >> $GITHUB_STEP_SUMMARY
echo "- Version folders with documentation content" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Timestamp:** $(date)" >> $GITHUB_STEP_SUMMARY