-
Notifications
You must be signed in to change notification settings - Fork 25
376 lines (322 loc) · 16 KB
/
merge-build-push.yml
File metadata and controls
376 lines (322 loc) · 16 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
name: Build , Push to web, Deploy Antora Docs
on:
pull_request_target:
types:
- closed
create:
workflow_dispatch:
inputs:
source_branch:
required: false
default: ""
jobs:
build-and-deploy:
runs-on: ubuntu-latest
if: github.event_name == 'workflow_dispatch' || github.event_name == 'create' || github.event.pull_request.merged == true
permissions:
contents: write
pull-requests: write
steps:
- name: PR was merged
if: github.event.pull_request.number != ''
run: |
echo "PR #${{ github.event.pull_request.number }} was merged into ${{ github.event.pull_request.base.ref }}."
echo "Head commit was: ${{ github.event.pull_request.head.sha }}"
- name: Checkout Documentation Repository (ivorysql_doc)
uses: actions/checkout@v4
with:
path: ivorysql_doc
ref: ${{ inputs.source_branch || github.ref }}
- name: Checkout Doc Builder Repository (doc_builder)
uses: actions/checkout@v4
with:
repository: ${{ github.repository_owner }}/ivory-doc-builder
path: ivory-doc-builder
- name: Determine Latest Version from ivorysql_docs branches
id: latest_version_step
shell: bash
run: |
echo "Detecting latest version from remote branches of IvorySQL/ivorysql_docs..."
LATEST_VERSION_NUMBER=$( \
git ls-remote --heads --refs "https://github.com/IvorySQL/ivorysql_docs.git" 'refs/heads/v*.*' | \
sed 's_^[^\t]*\trefs/heads/v__g' | \
grep -E '^[0-9]+\.[0-9]+(\.[0-9]+)?$' | \
sort -V | \
tail -n1 \
)
if [[ -z "$LATEST_VERSION_NUMBER" ]]; then
echo "::error::Could not determine latest version from branches. Please check git ls-remote command and repo accessibility."
exit 1
fi
echo "Detected latest version number: $LATEST_VERSION_NUMBER"
echo "version=$LATEST_VERSION_NUMBER" >> "$GITHUB_OUTPUT"
- name: Install yq
run: |
sudo apt-get update -y
sudo apt-get install -y jq
sudo wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/bin/yq
sudo chmod +x /usr/bin/yq
yq --version
- name: Modify Antora Playbooks
working-directory: ./ivory-doc-builder
env:
DETECTED_VERSION: '${{ steps.latest_version_step.outputs.version }}'
START_PAGE_COMPONENT_NAME: "ivorysql-doc"
START_PAGE_FILE_PATH: "welcome.adoc"
run: |
PLAYBOOK_FILES=("antora-playbook-CN.yml" "antora-playbook-EN.yml")
for PLAYBOOK_FILE in "${PLAYBOOK_FILES[@]}"; do
if [ -f "$PLAYBOOK_FILE" ]; then
echo "--- Modifying Playbook: $PLAYBOOK_FILE ---"
echo "Original content of $PLAYBOOK_FILE:"
cat "$PLAYBOOK_FILE"
echo # Newline for better readability
if [[ -n "$DETECTED_VERSION" ]]; then
# All branches use flat structure (single-level path): ivorysql-doc:v5.1:welcome.adoc
NEW_START_PAGE="${START_PAGE_COMPONENT_NAME}:v${DETECTED_VERSION}:${START_PAGE_FILE_PATH}"
yq -i ".site.start_page = \"$NEW_START_PAGE\"" "$PLAYBOOK_FILE"
echo "Updated .site.start_page in $PLAYBOOK_FILE to: $NEW_START_PAGE"
else
echo "WARNING: DETECTED_VERSION is empty. Skipping start_page update for $PLAYBOOK_FILE."
fi
# Add / support symbolic paths for the latest version + redirect:to
yq -i '.urls.latest_version_segment = "latest"' "$PLAYBOOK_FILE"
yq -i '.urls.latest_version_segment_strategy = "redirect:to"' "$PLAYBOOK_FILE"
echo "Set urls.latest_version_segment = latest"
echo "Set urls.latest_version_segment_strategy = redirect:to"
echo "Modified content of $PLAYBOOK_FILE:"
cat "$PLAYBOOK_FILE"
echo "--- Finished modification for $PLAYBOOK_FILE ---"
echo # Newline
else
echo "WARNING: Playbook file $PLAYBOOK_FILE not found in $(pwd)."
fi
done
- name: Pin PDF assembler to merged PR branch
working-directory: ./ivory-doc-builder
env:
MERGED_BRANCH: ${{ inputs.source_branch || github.event.pull_request.base.ref || github.ref_name }}
COMPONENT_NAME: ivorysql-doc
run: |
if [[ -z "${MERGED_BRANCH}" ]]; then
echo "::error::Merged branch name is empty, cannot update PDF component version."
exit 1
fi
TARGET_COMPONENT_VERSION="${MERGED_BRANCH}@${COMPONENT_NAME}"
echo "Setting antora-assembler.yml component_versions to ${TARGET_COMPONENT_VERSION}"
yq -i ".component_versions = \"${TARGET_COMPONENT_VERSION}\"" antora-assembler.yml
echo "Updated antora-assembler.yml:"
cat antora-assembler.yml
- name: Checkout Web Repository (web)
uses: actions/checkout@v4
with:
repository: ${{ github.repository_owner }}/ivorysql_web
path: www_publish_target
token: ${{ secrets.WEB_TOKEN }}
- name: Ensure web index redirects to latest home
id: update_web_index
working-directory: ./www_publish_target
env:
LATEST_VERSION: ${{ steps.latest_version_step.outputs.version }}
MERGED_PR_BASE: ${{ github.event.pull_request.base.ref || github.ref_name }}
run: |
set -euo pipefail
TARGET_BRANCH="v${LATEST_VERSION}"
# All versions use flat structure (single-level path): ivorysql-doc/v5.1/welcome.html
EXPECTED_PATH="ivorysql-doc/v${LATEST_VERSION}/welcome.html"
if [[ "${MERGED_PR_BASE}" != "${TARGET_BRANCH}" ]]; then
echo "Base branch ${MERGED_PR_BASE} is not the latest version branch ${TARGET_BRANCH}, skip index redirect check."
echo "index_updated=false" >> "$GITHUB_OUTPUT"
exit 0
fi
UPDATE_NEEDED=false
for lang in cn en; do
FILE_PATH="docs/${lang}/index.html"
if [[ ! -f "${FILE_PATH}" ]]; then
echo "Missing ${FILE_PATH}, cannot update redirect."
continue
fi
if grep -q "${EXPECTED_PATH}" "${FILE_PATH}"; then
echo "${FILE_PATH} already points to latest ${LATEST_VERSION}."
else
# Replace path to latest version
# Handle both old double-level and new single-level path formats
sed -i -E "s@ivorysql-doc/v[0-9]+(\\.[0-9]+){1,2}/(v[0-9]+(\\.[0-9]+){1,2}/)?welcome\\.html@${EXPECTED_PATH}@g" "${FILE_PATH}"
UPDATE_NEEDED=true
echo "Updated ${FILE_PATH} to latest ${LATEST_VERSION} redirect."
fi
done
echo "index_updated=${UPDATE_NEEDED}" >> "$GITHUB_OUTPUT"
- name: Setup Ruby and Bundler
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.0'
- name: Install Asciidoctor PDF and related Gems
run: |
echo "Installing Asciidoctor PDF gems..."
gem install asciidoctor-pdf --version "~>2.3.19"
gem install rouge
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '22.15'
- name: Install Antora CLI
run: |
echo "Installing Antora packages local..."
npm install --global [email protected] @antora/lunr-extension @antora/pdf-extension @node-rs/jieba
- name: Build English Documentation
working-directory: ./ivory-doc-builder
run: |
echo "Current directory: $(pwd)"
echo "Building English site..."
ls ../www_publish_target/
npx antora generate --stacktrace --to-dir ../www_publish_target/docs/en antora-playbook-EN.yml
- name: Copy English PDF export into web repo
working-directory: ./ivory-doc-builder
env:
MERGED_BRANCH: ${{ inputs.source_branch || github.event.pull_request.base.ref || github.ref_name }}
COMPONENT_NAME: ivorysql-doc
run: |
set -euo pipefail
if [[ -z "${MERGED_BRANCH}" ]]; then
echo "::error::Merged branch name is empty, cannot locate PDF output."
exit 1
fi
SOURCE_PDF_EN="build/assembler/pdf/${COMPONENT_NAME}/${MERGED_BRANCH}/_exports/index.pdf"
DEST_EN="../www_publish_target/docs/en/${COMPONENT_NAME}/${MERGED_BRANCH}/ivorysql.pdf"
echo "--- Debug: PDF output directory structure ---"
find build/ -name "*.pdf" 2>/dev/null || echo "No PDF files found under build/"
echo "--- End debug ---"
if [[ ! -f "${SOURCE_PDF_EN}" ]]; then
echo "::error::English PDF not found at ${SOURCE_PDF_EN}"
exit 1
fi
echo "Copying English PDF from ${SOURCE_PDF_EN} to web repo target..."
mkdir -p "$(dirname "${DEST_EN}")"
cp "${SOURCE_PDF_EN}" "${DEST_EN}"
echo "English PDF copied to:"
echo " - ${DEST_EN}"
- name: Build Chinese Documentation
working-directory: ./ivory-doc-builder
run: |
echo "Building Chinese site..."
npx antora generate --stacktrace --to-dir ../www_publish_target/docs/cn antora-playbook-CN.yml
- name: Copy Chinese PDF export into web repo
working-directory: ./ivory-doc-builder
env:
MERGED_BRANCH: ${{ inputs.source_branch || github.event.pull_request.base.ref || github.ref_name }}
COMPONENT_NAME: ivorysql-doc
run: |
set -euo pipefail
if [[ -z "${MERGED_BRANCH}" ]]; then
echo "::error::Merged branch name is empty, cannot locate PDF output."
exit 1
fi
SOURCE_PDF="build/assembler/pdf/${COMPONENT_NAME}/${MERGED_BRANCH}/_exports/index.pdf"
DEST_CN="../www_publish_target/docs/cn/${COMPONENT_NAME}/${MERGED_BRANCH}/ivorysql.pdf"
echo "--- Debug: PDF output directory structure ---"
find build/ -name "*.pdf" 2>/dev/null || echo "No PDF files found under build/"
echo "--- End debug ---"
if [[ ! -f "${SOURCE_PDF}" ]]; then
echo "::error::PDF not found at ${SOURCE_PDF}"
exit 1
fi
echo "Copying PDF from ${SOURCE_PDF} to web repo targets..."
mkdir -p "$(dirname "${DEST_CN}")"
cp "${SOURCE_PDF}" "${DEST_CN}"
echo "PDF copied to:"
echo " - ${DEST_CN}"
- name: Commit and Push to web Repository new branch , pull request
id: commit_push_new_branch
working-directory: ./www_publish_target
env:
OPEN_PUSH_PR: true
run: |
echo "push_pr=${OPEN_PUSH_PR}" >> $GITHUB_OUTPUT
echo "--- Preparing to commit and push changes ---"
echo "--- Git status ---"
GIT_STATUS_OUTPUT=$(git status --porcelain)
echo "${GIT_STATUS_OUTPUT}"
echo "--- End of git status --porcelain output ---"
git config user.name "IvorySQL Actions Bot"
git config user.email "[email protected]"
if [ -z "${GIT_STATUS_OUTPUT}" ]; then
echo "No changes to commit."
echo "changes_detected=false" >> $GITHUB_OUTPUT
else
echo "Changes detected. Proceeding with add, commit, and push."
if [[ "${OPEN_PUSH_PR}" == "true" ]]; then
NEW_BRANCH_NAME="docs-update-${{ github.run_attempt }}-$(date +'%Y-%m-%d-%H%M%S')"
echo "Generated new branch name: ${NEW_BRANCH_NAME}"
echo "new_branch_name=${NEW_BRANCH_NAME}" >> $GITHUB_OUTPUT
echo "changes_detected=true" >> $GITHUB_OUTPUT
git checkout -b "${NEW_BRANCH_NAME}"
git add .
COMMIT_MESSAGE="docs: Regenerate Antora site from IvorySQL/ivorysql_docs commit ${{ github.event.head_commit.id || github.sha }}"
git commit -m "${COMMIT_MESSAGE}"
git push origin "${NEW_BRANCH_NAME}"
else
echo "Pushing changes to master branch."
git add .
COMMIT_MESSAGE="docs: Regenerate Antora site from IvorySQL/ivorysql_docs commit ${{ github.event.head_commit.id || github.sha }}"
git commit -m "${COMMIT_MESSAGE}"
git push origin master
fi
fi
- name: Create or Update Pull Request with GitHub CLI
id: cpr
if: steps.commit_push_new_branch.outputs.push_pr == 'true' && steps.commit_push_new_branch.outputs.changes_detected == 'true' && steps.commit_push_new_branch.outputs.new_branch_name != ''
working-directory: ./www_publish_target
env:
GH_TOKEN: ${{ secrets.WEB_TOKEN }}
PARAM_BASE_BRANCH: master
PARAM_HEAD_BRANCH: ${{ steps.commit_push_new_branch.outputs.new_branch_name }}
PARAM_TITLE: "Docs: Automated update from IvorySQL/ivorysql_docs commit ${{ github.event.head_commit.id || github.sha }}"
PARAM_BODY: |
Automated Antora site regeneration based on changes in the IvorySQL/ivorysql_docs repository.
Source commit: `${{ github.server_url }}/${{ github.repository_owner }}/ivorysql_docs/commit/${{ github.event.head_commit.id || github.sha }}`
- Auto-generated by gh pr create
PARAM_DRAFT: "false"
run: |
echo "Attempting to create or update Pull Request for branch '${PARAM_HEAD_BRANCH}' into '${PARAM_BASE_BRANCH}'"
DRAFT_FLAG_STRING=""
if [[ "${PARAM_DRAFT}" == "true" ]]; then
DRAFT_FLAG_STRING="--draft"
fi
echo "Executing: gh pr create --base \"${PARAM_BASE_BRANCH}\" --head \"${PARAM_HEAD_BRANCH}\" --title <title> --body-file <(echo body) ${DRAFT_FLAG_STRING}"
if gh pr create \
--base "${PARAM_BASE_BRANCH}" \
--head "${PARAM_HEAD_BRANCH}" \
--title "${PARAM_TITLE}" \
--body-file <(echo "${PARAM_BODY}") \
${DRAFT_FLAG_STRING}; then
echo "Pull Request created or already exists and metadata might have been updated."
else
echo "'gh pr create' command indicated an issue or no action was taken."
EXISTING_PR_URL=$(gh pr view "${PARAM_HEAD_BRANCH}" --json url -q ".url" 2>/dev/null || echo "")
if [[ -n "$EXISTING_PR_URL" ]]; then
echo "An existing PR was found for branch '${PARAM_HEAD_BRANCH}': ${EXISTING_PR_URL}"
echo "Proceeding to enable auto-merge for this existing PR."
else
echo "::error::Failed to create PR and no existing PR found for branch '${PARAM_HEAD_BRANCH}'. Cannot enable auto-merge."
exit 1
fi
fi
- name: Enable Auto-Merge for PR
if: steps.cpr.outcome == 'success' && steps.commit_push_new_branch.outputs.push_pr == 'true' && steps.commit_push_new_branch.outputs.changes_detected == 'true' && steps.commit_push_new_branch.outputs.new_branch_name != ''
working-directory: ./www_publish_target
env:
GH_TOKEN: ${{ secrets.WEB_TOKEN }}
PR_HEAD_BRANCH: ${{ steps.commit_push_new_branch.outputs.new_branch_name }}
PR_BASE_BRANCH: master
MERGE_STRATEGY: MERGE
run: |
echo "Attempting to enable auto-merge for PR from branch '$PR_HEAD_BRANCH' to '$PR_BASE_BRANCH'"
if gh pr merge "$PR_HEAD_BRANCH" \
--${MERGE_STRATEGY,,} \
--delete-branch; then
echo "Auto-merge enabled successfully for PR from branch '$PR_HEAD_BRANCH'."
else
echo "::warning::Failed to enable auto-merge for PR from branch '$PR_HEAD_BRANCH'."
echo "This might be because the PR is not mergeable (e.g., has conflicts, is a draft PR), requires reviews, auto-merge is already enabled, or the PR could not be uniquely identified by branch name."
fi