Repository navigation
248 lines (218 loc) · 9.33 KB
/
Copy pathrelease-website.yml
File metadata and controls
248 lines (218 loc) · 9.33 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
name: Release Website
on:
release:
types: [published]
workflow_dispatch:
inputs:
version:
description: 'Version number (e.g. 2.0.5 or 2.1.0-alpha.1)'
required: true
jobs:
versioning:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout main
uses: actions/checkout@v4
with:
ref: main
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
cache-dependency-path: ./website/package-lock.json
- name: Install dependencies
working-directory: ./website
run: npm ci
- name: Determine Version
id: determine_version
env:
EVENT_NAME: ${{ github.event_name }}
TAG_NAME: ${{ github.ref_name }}
RELEASE_IS_PRERELEASE: ${{ github.event.release.prerelease }}
INPUT_VERSION: ${{ inputs.version }}
run: |
# 1. Get raw version number (e.g. 2.0.5 or 2.1.0-alpha.1)
if [ "$EVENT_NAME" = "release" ]; then
RAW_VERSION="${TAG_NAME#v}"
IS_PRERELEASE="$RELEASE_IS_PRERELEASE"
else
RAW_VERSION="$INPUT_VERSION"
if [[ "$RAW_VERSION" == *-* ]]; then
IS_PRERELEASE="true"
else
IS_PRERELEASE="false"
fi
fi
echo "Detected raw version: $RAW_VERSION"
echo "Pre-release: $IS_PRERELEASE"
# 2. Validate the version format
if ! [[ "$RAW_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
echo "::error::Invalid version: ${RAW_VERSION}. Expected format like 2.0.5 or 2.1.0-alpha.1"
exit 1
fi
# 3. Validate GitHub release type vs semver
if [ "$EVENT_NAME" = "release" ]; then
if [ "$IS_PRERELEASE" = "true" ] && [[ "$RAW_VERSION" != *-* ]]; then
echo "::error::GitHub pre-release requires a semver prerelease segment (e.g. 2.1.0-alpha.1)"
exit 1
fi
if [ "$IS_PRERELEASE" != "true" ] && [[ "$RAW_VERSION" == *-* ]]; then
echo "::error::Stable GitHub release must not use a prerelease version (${RAW_VERSION}). Mark the release as pre-release or use a stable version."
exit 1
fi
fi
# 4. Convert to Docusaurus doc version
if [ "$IS_PRERELEASE" = "true" ]; then
DOC_VERSION=$(node -e '
const version = process.argv[1];
const m = version.match(/^(\d+\.\d+\.\d+)-([0-9A-Za-z-]+)/);
if (!m) {
console.error("::error::Could not derive doc version from prerelease (" + version + "). Expected format like 2.1.0-alpha.1");
process.exit(1);
}
process.stdout.write(m[1] + "-" + m[2] + ".x");
' "$RAW_VERSION")
else
BASE_VER=${RAW_VERSION%.*}
DOC_VERSION="${BASE_VER}.x"
fi
# 5. Build the pull request texts. They contain ": ", which is not valid inside
# an unquoted YAML value in the create-pull-request step.
if [ "$IS_PRERELEASE" = "true" ]; then
PR_TITLE="docs: update pre-release version ${DOC_VERSION} (${RAW_VERSION})"
PR_BODY="Automated docs update for pre-release ${DOC_VERSION} (tag version ${RAW_VERSION})."
else
PR_TITLE="docs: update version ${DOC_VERSION}"
PR_BODY="Automated docs update for version ${DOC_VERSION}."
fi
echo "Target Docusaurus Version: $DOC_VERSION"
echo "doc_version=$DOC_VERSION" >> "$GITHUB_OUTPUT"
echo "is_prerelease=$IS_PRERELEASE" >> "$GITHUB_OUTPUT"
echo "raw_version=$RAW_VERSION" >> "$GITHUB_OUTPUT"
echo "pr_title=$PR_TITLE" >> "$GITHUB_OUTPUT"
echo "pr_body=$PR_BODY" >> "$GITHUB_OUTPUT"
- name: Remove prerelease doc versions on stable release
if: steps.determine_version.outputs.is_prerelease != 'true'
working-directory: ./website
env:
RAW_VERSION: ${{ steps.determine_version.outputs.raw_version }}
run: |
VERSIONS_JSON="versions.json"
remove_doc_version() {
local v="$1"
local VERSION_DIR="versioned_docs/version-$v"
local SIDEBAR_FILE="versioned_sidebars/version-$v-sidebars.json"
echo "Removing prerelease doc version: $v"
if [ -d "$VERSION_DIR" ]; then
rm -rf "$VERSION_DIR"
echo "Removed $VERSION_DIR"
fi
if [ -f "$SIDEBAR_FILE" ]; then
rm -f "$SIDEBAR_FILE"
echo "Removed $SIDEBAR_FILE"
fi
if [ -f "$VERSIONS_JSON" ]; then
tmp=$(mktemp)
jq --arg v "$v" 'map(select(. != $v))' "$VERSIONS_JSON" > "$tmp" && mv "$tmp" "$VERSIONS_JSON"
echo "Removed $v from $VERSIONS_JSON"
fi
}
# Any prerelease docs whose base version is at or below the stable release are
# superseded by it, e.g. stable 2.1.1 removes 2.1.0-alpha.x and 2.1.1-beta.x.
PRERELEASE_VERSIONS=$(
{
if [ -f "$VERSIONS_JSON" ]; then
jq -r '.[]' "$VERSIONS_JSON"
fi
shopt -s nullglob
for dir in versioned_docs/version-*; do
basename "$dir" | sed 's/^version-//'
done
} | sort -u | node -e '
const stable = process.env.RAW_VERSION.split("-")[0].split(".").map(Number);
const isSuperseded = (base) => {
for (let i = 0; i < 3; i++) {
if (base[i] !== stable[i]) return base[i] < stable[i];
}
return true;
};
for (const line of require("fs").readFileSync(0, "utf8").split("\n")) {
const docVersion = line.trim();
const m = docVersion.match(/^(\d+)\.(\d+)\.(\d+)-[0-9A-Za-z-]+\.x$/);
if (!m) continue;
if (isSuperseded(m.slice(1, 4).map(Number))) console.log(docVersion);
}
'
)
if [ -z "$PRERELEASE_VERSIONS" ]; then
echo "No prerelease doc versions found for stable release ${RAW_VERSION}."
exit 0
fi
while IFS= read -r v; do
[ -n "$v" ] && remove_doc_version "$v"
done <<< "$PRERELEASE_VERSIONS"
- name: Clean up existing version (if any)
working-directory: ./website
env:
DOC_VERSION: ${{ steps.determine_version.outputs.doc_version }}
run: |
VERSION_DIR="versioned_docs/version-$DOC_VERSION"
SIDEBAR_FILE="versioned_sidebars/version-$DOC_VERSION-sidebars.json"
VERSIONS_JSON="versions.json"
if [ -d "$VERSION_DIR" ]; then
echo "Version $DOC_VERSION already exists. Cleaning up to overwrite..."
# 1. Remove documentation directory
rm -rf "$VERSION_DIR"
echo "Removed $VERSION_DIR"
# 2. Remove sidebar file
rm -f "$SIDEBAR_FILE"
echo "Removed $SIDEBAR_FILE"
# 3. Remove entry from versions.json using jq
# We filter out the current DOC_VERSION to reset the list state
if [ -f "$VERSIONS_JSON" ]; then
tmp=$(mktemp)
jq --arg v "$DOC_VERSION" 'map(select(. != $v))' "$VERSIONS_JSON" > "$tmp" && mv "$tmp" "$VERSIONS_JSON"
echo "Removed $DOC_VERSION from $VERSIONS_JSON"
fi
else
echo "Version $DOC_VERSION does not exist yet. Ready to create."
fi
# The pull request has to be based on main, but the snapshot must contain the docs
# as they were at the released tag, not whatever landed on main afterwards.
- name: Check Out Docs From the Released Tag
if: github.event_name == 'release'
working-directory: ./website
env:
TAG_NAME: ${{ github.ref_name }}
run: |
if git rev-parse --verify "refs/tags/${TAG_NAME}" > /dev/null 2>&1; then
git checkout "refs/tags/${TAG_NAME}" -- docs sidebars.ts
echo "Using docs from tag ${TAG_NAME}."
else
echo "::warning::Tag ${TAG_NAME} not found; falling back to the docs on main."
fi
- name: Run Docusaurus Versioning
working-directory: ./website
env:
DOC_VERSION: ${{ steps.determine_version.outputs.doc_version }}
run: |
echo "Creating version $DOC_VERSION..."
npm run docusaurus docs:version "$DOC_VERSION"
- name: Restore Docs From main
if: github.event_name == 'release'
working-directory: ./website
run: git restore --source=HEAD --staged --worktree -- docs sidebars.ts
- name: Create Pull Request
uses: peter-evans/create-pull-request@v6
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: ${{ steps.determine_version.outputs.pr_title }}
title: ${{ steps.determine_version.outputs.pr_title }}
body: ${{ steps.determine_version.outputs.pr_body }}
branch: docs-version-${{ steps.determine_version.outputs.doc_version }}
base: main