-
Notifications
You must be signed in to change notification settings - Fork 0
374 lines (316 loc) · 13.3 KB
/
Copy pathbuild-and-deploy.yml
File metadata and controls
374 lines (316 loc) · 13.3 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
name: Build and Deploy Docusaurus Documentation
on:
repository_dispatch:
types: [docs-update, docs-version-deploy]
workflow_dispatch:
inputs:
version:
description: 'Version to build (latest, v0.2.23, etc.)'
required: false
default: 'latest'
type: string
action:
description: 'Action to perform'
required: false
default: 'build-and-deploy'
type: choice
options:
- build-only
- deploy-only
- build-and-deploy
ogx_branch:
description: 'Branch of ogx to build from (default: main)'
required: false
default: 'main'
type: string
concurrency:
group: docs-build-deploy
cancel-in-progress: false
permissions:
contents: write
pages: write
id-token: write
jobs:
build:
if: ${{ github.event.inputs.action != 'deploy-only' }}
runs-on: ubuntu-latest
outputs:
building_latest: ${{ steps.set-version.outputs.building_latest }}
steps:
- name: Checkout this repository
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Clone ogx repository
id: set-version
run: |
echo "Cloning ogx repository..."
TEMP_DIR=$(mktemp -d)
echo "TEMP_DIR=$TEMP_DIR" >> $GITHUB_ENV
git clone https://github.com/${{ github.repository_owner }}/ogx.git "$TEMP_DIR/ogx"
cd "$TEMP_DIR/ogx"
git fetch --all --tags
# Determine version to checkout
VERSION="${{ github.event.client_payload.version || github.event.inputs.version || 'latest' }}"
if [ "$VERSION" = "latest" ] || [ -z "$VERSION" ]; then
LSBRANCH="${{ github.event.inputs.ogx_branch || 'main' }}"
LSBRANCH="${LSBRANCH:-main}"
git checkout "$LSBRANCH"
echo "Using branch: $LSBRANCH"
echo "BUILDING_LATEST=true" >> $GITHUB_ENV
echo "building_latest=true" >> $GITHUB_OUTPUT
echo "VERSION_TAG=latest" >> $GITHUB_ENV
else
git checkout "$VERSION"
echo "Using tag: $VERSION"
echo "BUILDING_LATEST=false" >> $GITHUB_ENV
echo "building_latest=false" >> $GITHUB_OUTPUT
echo "VERSION_TAG=$VERSION" >> $GITHUB_ENV
fi
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
# ──────────────────────────────────────────────
# Latest build path
# ──────────────────────────────────────────────
- name: Setup caching for build artifacts
if: env.BUILDING_LATEST == 'true'
uses: actions/cache@v4
id: cache-npm
with:
path: ~/.npm
key: npm-${{ runner.os }}-${{ hashFiles('${{ env.TEMP_DIR }}/ogx/docs/package-lock.json') }}
restore-keys: |
npm-${{ runner.os }}-
- name: Install dependencies and setup versioning
if: env.BUILDING_LATEST == 'true'
run: |
echo "Installing dependencies..."
cd "${{ env.TEMP_DIR }}/ogx/docs"
npm ci
echo "Dependencies installed"
echo "Setting up versioning configuration..."
# Copy persistent versionsArchived.json from repo
cp "${{ github.workspace }}/versionsArchived.json" ./
# Archived versions are served as standalone static HTML under docs/vX.Y.Z/
# Docusaurus versioning is not used - always write empty versions.json
echo "[]" > versions.json
# Fix GitHub org to match this repo's owner
sed -i "s|https://github.com/ogx-ai/ogx|https://github.com/${{ github.repository_owner }}/ogx|g" docusaurus.config.ts
# Restrict blog to top-level .md/.mdx files only (exclude subdirectory files)
node -e "
const fs = require('fs');
let c = fs.readFileSync('docusaurus.config.ts', 'utf8');
if (!c.match(/blog[\s\S]*?include:/)) {
c = c.replace(/(blog:\s*\{)/, \"\\\$1\\n include: ['*.{md,mdx}'],\");
fs.writeFileSync('docusaurus.config.ts', c);
console.log('Blog include pattern set to top-level only');
} else {
console.log('Blog already has explicit include pattern');
}
"
# Replace docsVersionDropdown with a custom dropdown populated from versionsArchived.json
node -e "
const fs = require('fs');
const archived = JSON.parse(fs.readFileSync('versionsArchived.json', 'utf8'));
let c = fs.readFileSync('docusaurus.config.ts', 'utf8');
// Build dropdown items from archived versions
const items = Object.entries(archived).map(([tag, url]) =>
' { label: \"' + tag + '\", href: \"' + url + '\" }'
).join(',\n');
// Replace the docsVersionDropdown block with a custom dropdown
c = c.replace(
/\{\s*type:\s*'docsVersionDropdown'[\s\S]*?\n\s{8}\},\n/,
'{\n' +
' type: \"dropdown\",\n' +
' label: \"main (unreleased)\",\n' +
' position: \"right\",\n' +
' items: [\n' +
items + ',\n' +
' { to: \"/versions\", label: \"All versions\" },\n' +
' ],\n' +
' },\n'
);
fs.writeFileSync('docusaurus.config.ts', c);
console.log('Version dropdown patched with ' + Object.keys(archived).length + ' archived versions');
"
echo "Configuration patched"
- name: Cache stable API docs
if: env.BUILDING_LATEST == 'true'
uses: actions/cache@v4
id: cache-api-stable
with:
path: ${{ env.TEMP_DIR }}/ogx/docs/docs/api
key: api-stable-${{ hashFiles('${{ env.TEMP_DIR }}/ogx/docs/static/ogx-spec.yaml') }}
restore-keys: api-stable-
- name: Cache experimental API docs
if: env.BUILDING_LATEST == 'true'
uses: actions/cache@v4
id: cache-api-experimental
with:
path: ${{ env.TEMP_DIR }}/ogx/docs/docs/api-experimental
key: api-experimental-${{ hashFiles('${{ env.TEMP_DIR }}/ogx/docs/static/experimental-ogx-spec.yaml') }}
restore-keys: api-experimental-
- name: Cache deprecated API docs
if: env.BUILDING_LATEST == 'true'
uses: actions/cache@v4
id: cache-api-deprecated
with:
path: ${{ env.TEMP_DIR }}/ogx/docs/docs/api-deprecated
key: api-deprecated-${{ hashFiles('${{ env.TEMP_DIR }}/ogx/docs/static/deprecated-ogx-spec.yaml') }}
restore-keys: api-deprecated-
- name: Generate API docs
if: env.BUILDING_LATEST == 'true'
run: |
cd "${{ env.TEMP_DIR }}/ogx/docs"
echo "Generating API docs in parallel..."
PIDS=()
if [ "${{ steps.cache-api-stable.outputs.cache-hit }}" != 'true' ]; then
npm run gen-api-docs stable &
PIDS+=($!)
fi
if [ "${{ steps.cache-api-experimental.outputs.cache-hit }}" != 'true' ]; then
npm run gen-api-docs experimental &
PIDS+=($!)
fi
if [ "${{ steps.cache-api-deprecated.outputs.cache-hit }}" != 'true' ]; then
npm run gen-api-docs deprecated &
PIDS+=($!)
fi
for pid in "${PIDS[@]}"; do
wait $pid || exit 1
done
echo "API docs generation completed"
- name: Build Docusaurus site
if: env.BUILDING_LATEST == 'true'
run: |
cd "${{ env.TEMP_DIR }}/ogx/docs"
# Patch out duplicate version badges from OpenAPI MDX files
find . -name "ogx-specification.info.mdx" -type f -exec sed -i '/<span$/,/<\/span>$/d' {} \;
echo "Running Docusaurus build..."
npm run build
echo "Docusaurus build completed"
env:
NODE_OPTIONS: "--max-old-space-size=10240"
- name: Deploy latest build to docs/
if: env.BUILDING_LATEST == 'true'
run: |
DOCS_DIR="${{ github.workspace }}/docs"
# Clear everything except .git, .nojekyll, and archived versions (static HTML under v*)
find "$DOCS_DIR" -mindepth 1 -maxdepth 1 ! -name '.git' ! -name '.nojekyll' ! -name 'v[0-9]*' -exec rm -rf {} +
# Copy Docusaurus build output
cp -r "${{ env.TEMP_DIR }}/ogx/docs/build/"* "$DOCS_DIR/"
# Copy versioning files
cp "${{ github.workspace }}/versionsArchived.json" "$DOCS_DIR/"
echo "[]" > "$DOCS_DIR/versions.json"
touch "$DOCS_DIR/.nojekyll"
echo "Latest build deployed to docs/"
# ──────────────────────────────────────────────
# Versioned (archived) build path
# ──────────────────────────────────────────────
- name: Build archived version
if: env.BUILDING_LATEST != 'true'
run: |
./build-archived-version.sh "${{ env.VERSION_TAG }}" --ogx-dir "${{ env.TEMP_DIR }}/ogx"
- name: Update versionsArchived.json
if: env.BUILDING_LATEST != 'true'
run: |
TAG="${{ env.VERSION_TAG }}"
python3 -c "
import json
with open('versionsArchived.json') as f:
archived = json.load(f)
tag = '${TAG}'
url = 'https://ogx-ai.github.io/${TAG}/'
if tag not in archived:
archived[tag] = url
def version_key(item):
parts = item[0].lstrip('v').split('.')
return [int(p) for p in parts if p.isdigit()]
archived = dict(sorted(archived.items(), key=version_key, reverse=True))
with open('versionsArchived.json', 'w') as f:
json.dump(archived, f, indent=2)
f.write('\n')
"
# Also update the deployed copy
cp versionsArchived.json docs/
- name: Commit archived version
if: env.BUILDING_LATEST != 'true'
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add -f "docs/${{ env.VERSION_TAG }}/" versionsArchived.json docs/versionsArchived.json
git diff --staged --quiet || git commit -s -m "docs: add archived version ${{ env.VERSION_TAG }}"
git pull --rebase origin main
git push
# ──────────────────────────────────────────────
# Common: verify and upload Pages artifact
# ──────────────────────────────────────────────
- name: Verify deployment structure
run: |
echo "Contents of docs directory:"
ls -la "${{ github.workspace }}/docs/" | head -10
echo -e "\nVersioning files:"
[ -f "${{ github.workspace }}/docs/versionsArchived.json" ] && echo "versionsArchived.json exists" || echo "versionsArchived.json missing"
- name: Setup GitHub Pages
if: ${{ (github.event.inputs.action == 'build-and-deploy' || github.event.inputs.action == '' || github.event_name == 'repository_dispatch') && (env.BUILDING_LATEST == 'true' || github.event_name != 'repository_dispatch') }}
uses: actions/configure-pages@v5
- name: Upload Pages artifact
if: ${{ (github.event.inputs.action == 'build-and-deploy' || github.event.inputs.action == '' || github.event_name == 'repository_dispatch') && (env.BUILDING_LATEST == 'true' || github.event_name != 'repository_dispatch') }}
uses: actions/upload-pages-artifact@v3
with:
path: 'docs'
deploy:
if: ${{ (github.event.inputs.action == 'build-and-deploy' || github.event.inputs.action == '' || github.event_name == 'repository_dispatch') && needs.build.outputs.building_latest != 'false' }}
runs-on: ubuntu-latest
needs: build
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
rebuild-latest:
# After committing an archived version, trigger a full latest rebuild so the
# Pages deployment includes both the main site and all archived versions.
if: ${{ needs.build.outputs.building_latest == 'false' }}
runs-on: ubuntu-latest
needs: build
steps:
- name: Trigger latest docs rebuild
uses: peter-evans/repository-dispatch@v3
with:
event-type: docs-update
deploy-only:
if: ${{ github.event.inputs.action == 'deploy-only' }}
runs-on: ubuntu-latest
permissions:
contents: read
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup GitHub Pages
uses: actions/configure-pages@v5
- name: Upload Pages artifact
uses: actions/upload-pages-artifact@v3
with:
path: 'docs'
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4