Skip to content

Commit cb9f8bc

Browse files
feat: keep multiple versions of github pages (#255)
When we started hosting our own version of the docs, we had to change the github workflow to build the docs to an org-approved one, which handled things differently. It was using the "deploy from github actions" method instead of "deploy from branch". This PR adds what's necessary to keep all docs versions in [this branch](https://github.com/famedly/synapse/tree/gh-pages), which allows the version dropdown picker to work. Adding/removing folders in this gh-pages branch allows to alter what's shown in the version picker.
2 parents be8156c + cf0a121 commit cb9f8bc

1 file changed

Lines changed: 51 additions & 42 deletions

File tree

.github/workflows/docs.yaml

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ on:
1010

1111
workflow_dispatch:
1212

13-
permissions:
14-
contents: read
15-
pages: write
16-
id-token: write
17-
1813
jobs:
1914
pre:
2015
name: Calculate variables for GitHub Pages deployment
@@ -32,7 +27,7 @@ jobs:
3227
3328
case $branch in
3429
famedly-release/v*)
35-
# strip 'release-' from the name for release branches.
30+
# strip 'famedly-release/v' from the name for release branches.
3631
branch="${branch#famedly-release/v}"
3732
;;
3833
master)
@@ -47,11 +42,16 @@ jobs:
4742
branch-version: ${{ steps.vars.outputs.branch-version }}
4843

4944
################################################################################
50-
build:
45+
pages-docs:
5146
name: GitHub Pages
5247
runs-on: ubuntu-latest
5348
needs:
5449
- pre
50+
concurrency:
51+
group: "pages"
52+
cancel-in-progress: false
53+
permissions:
54+
contents: write
5555

5656
steps:
5757
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
@@ -67,15 +67,11 @@ jobs:
6767

6868
- name: Install required tooling
6969
env:
70-
MDBOOK_VERSION: "0.5.2"
70+
MDBOOK_VERSION: "0.5.2"
7171
run: |
7272
cargo install mdbook-linkcheck mdbook-mermaid
7373
cargo install mdbook --no-default-features --features search --vers "${{ env.MDBOOK_VERSION }}" --locked
7474
75-
- name: Setup Pages
76-
id: pages
77-
uses: actions/configure-pages@v5
78-
7975
- name: Set version of docs
8076
run: echo 'window.SYNAPSE_VERSION = "${{ needs.pre.outputs.branch-version }}";' > ./docs/website_files/version.js
8177

@@ -106,38 +102,51 @@ jobs:
106102
yq < schema/synapse-config.schema.yaml \
107103
> book/schema/synapse-config.schema.json
108104
109-
- name: Restructure for versioning
105+
# Deploy to the target directory without wiping other versions.
106+
# We use a git worktree so we can merge into gh-pages incrementally,
107+
# equivalent to what peaceiris/actions-gh-pages (action used by element/synapse)
108+
# does with destination_dir.
109+
#
110+
# Important: this workflow publishes by pushing commits directly to the
111+
# gh-pages branch. The repository's GitHub Pages settings must therefore
112+
# be configured to deploy from the gh-pages branch. If Pages is configured
113+
# to deploy from "GitHub Actions", this workflow will push commits but the
114+
# site will stop publishing.
115+
- name: Deploy to gh-pages
110116
run: |
111-
# Create a clean output directory
112-
mkdir -p _site/${{ needs.pre.outputs.branch-version }}
117+
git config user.name "github-actions[bot]"
118+
git config user.email "github-actions[bot]@users.noreply.github.com"
119+
120+
VERSION="${{ needs.pre.outputs.branch-version }}"
121+
122+
# Set up a worktree pointing at the gh-pages branch (create it if absent).
123+
# Use ls-remote to check existence before fetching so that a missing
124+
# branch is treated as "bootstrap" while any real fetch error still
125+
# fails the job.
126+
if git ls-remote --heads --exit-code origin gh-pages > /dev/null; then
127+
git fetch origin gh-pages
128+
git worktree add gh-pages-dir origin/gh-pages
129+
else
130+
# Branch doesn't exist yet, create an empty root commit.
131+
EMPTY_TREE=$(git hash-object -t tree /dev/null)
132+
INIT_COMMIT=$(git commit-tree "$EMPTY_TREE" -m "Initialize gh-pages")
133+
git branch gh-pages "$INIT_COMMIT"
134+
git worktree add gh-pages-dir gh-pages
135+
fi
113136
114-
# Move the built book (and schemas) into the versioned subdirectory
115-
mv book/* _site/${{ needs.pre.outputs.branch-version }}/
137+
# Replace only this version's directory, preserving all others.
138+
rm -rf "gh-pages-dir/$VERSION"
139+
mkdir -p "gh-pages-dir/$VERSION"
140+
cp -r book/. "gh-pages-dir/$VERSION/"
116141
117-
# Create a root index.html that redirects to the versioned docs
118-
# This ensures https://famedly.github.io/synapse/ still works
119-
if [ "${{ needs.pre.outputs.branch-version }}" = "latest" ]; then
120-
echo '<!DOCTYPE html><meta http-equiv="refresh" content="0; url=latest/index.html">' > _site/index.html
142+
# Keep the root redirect pointing at latest/, creating or updating it
143+
# whenever latest is deployed or on initial bootstrap.
144+
if [ "$VERSION" = "latest" ] || [ ! -f "gh-pages-dir/index.html" ]; then
145+
echo '<!DOCTYPE html><meta http-equiv="refresh" content="0; url=latest/index.html">' \
146+
> "gh-pages-dir/index.html"
121147
fi
122148
123-
- name: Upload artifact
124-
uses: actions/upload-pages-artifact@v3
125-
with:
126-
path: ./_site
127-
128-
deploy:
129-
if: ${{ github.ref == 'refs/heads/master' }}
130-
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
131-
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
132-
concurrency:
133-
group: "pages"
134-
cancel-in-progress: false
135-
environment:
136-
name: github-pages
137-
url: ${{ steps.deployment.outputs.page_url }}
138-
runs-on: ubuntu-latest
139-
needs: build
140-
steps:
141-
- name: Deploy to GitHub Pages
142-
id: deployment
143-
uses: actions/deploy-pages@v4
149+
cd gh-pages-dir
150+
git add .
151+
git diff --staged --quiet || git commit -m "docs: deploy $VERSION"
152+
git push origin HEAD:gh-pages

0 commit comments

Comments
 (0)