Skip to content

Commit 6ea463e

Browse files
committed
Use workflow from integrate-design-documents branch
1 parent 6b91e98 commit 6ea463e

1 file changed

Lines changed: 92 additions & 1 deletion

File tree

.github/workflows/mkdocs.yml

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ on:
44
branches: [ master, development ]
55
pull_request:
66
branches: [ master, development ]
7+
types: [ opened, synchronize, reopened, closed ]
8+
9+
permissions:
10+
contents: write
11+
pull-requests: write
712

813
env:
914
REPO_METADATA_SHAS: repo-metadata-shas.txt
1015

1116
jobs:
1217
build:
18+
if: github.event.action != 'closed'
1319
runs-on: ubuntu-latest
1420
steps:
1521
- uses: actions/checkout@v4
@@ -21,7 +27,7 @@ jobs:
2127
timeout_minutes: 15
2228
command: |
2329
sudo apt-get update
24-
sudo apt-get install -yqq build-essential libxml2-dev zlib1g-dev bison flex
30+
sudo apt-get install -yqq build-essential libxml2-dev zlib1g-dev bison flex libcairo2-dev pkg-config
2531
2632
- name: Set up Python install env
2733
run: |
@@ -51,6 +57,68 @@ jobs:
5157
export GITHUB_TOKEN=${{ github.token }}
5258
make build
5359
60+
- name: Deploy branch preview
61+
if: github.event_name == 'pull_request'
62+
run: |
63+
BRANCH_NAME="${{ github.head_ref }}"
64+
SAFE_BRANCH=$(echo "$BRANCH_NAME" | sed 's/[^a-zA-Z0-9-]/-/g')
65+
66+
. venv/bin/activate
67+
68+
# Build with site_url set so navigation works in subdirectory
69+
cat > mkdocs-preview.yml << EOF
70+
INHERIT: mkdocs.yml
71+
site_url: https://docs.openworm.org/preview/$SAFE_BRANCH/
72+
EOF
73+
python -m mkdocs build -f mkdocs-preview.yml --site-dir /tmp/site_preview
74+
75+
# Push to gh-pages under preview/<branch>/
76+
git fetch origin gh-pages:gh-pages
77+
git config user.name github-actions
78+
git config user.email github-actions@github.com
79+
git checkout gh-pages
80+
81+
mkdir -p preview/$SAFE_BRANCH
82+
rm -rf preview/$SAFE_BRANCH/*
83+
cp -r /tmp/site_preview/* preview/$SAFE_BRANCH/
84+
85+
git add preview/$SAFE_BRANCH
86+
git commit -m "Deploy preview: $BRANCH_NAME @ ${GITHUB_SHA::7} [ci skip]" || echo "No changes to commit"
87+
git remote set-url origin https://${{ github.token }}@github.com/${{ github.repository_owner }}/openworm_docs.git
88+
git push origin gh-pages
89+
90+
- name: Comment preview URL on PR
91+
if: github.event_name == 'pull_request'
92+
uses: actions/github-script@v7
93+
with:
94+
script: |
95+
const branch = '${{ github.head_ref }}'.replace(/[^a-zA-Z0-9-]/g, '-');
96+
const url = `https://docs.openworm.org/preview/${branch}/`;
97+
const marker = '<!-- preview-url -->';
98+
const body = `${marker}\n**Preview:** ${url}`;
99+
100+
const { data: comments } = await github.rest.issues.listComments({
101+
owner: context.repo.owner,
102+
repo: context.repo.repo,
103+
issue_number: context.issue.number,
104+
});
105+
const existing = comments.find(c => c.body.includes(marker));
106+
if (existing) {
107+
await github.rest.issues.updateComment({
108+
owner: context.repo.owner,
109+
repo: context.repo.repo,
110+
comment_id: existing.id,
111+
body,
112+
});
113+
} else {
114+
await github.rest.issues.createComment({
115+
owner: context.repo.owner,
116+
repo: context.repo.repo,
117+
issue_number: context.issue.number,
118+
body,
119+
});
120+
}
121+
54122
- name: Archive production artifacts
55123
if: github.ref == 'refs/heads/master'
56124
uses: actions/upload-artifact@v4
@@ -87,3 +155,26 @@ jobs:
87155
git remote set-url origin https://${{ github.token }}@github.com/${{ github.repository_owner }}/openworm_docs.git
88156
git push -f origin gh-pages
89157
fi
158+
159+
cleanup-preview:
160+
if: github.event_name == 'pull_request' && github.event.action == 'closed'
161+
runs-on: ubuntu-latest
162+
steps:
163+
- uses: actions/checkout@v4
164+
- name: Remove preview from gh-pages
165+
run: |
166+
BRANCH_NAME="${{ github.head_ref }}"
167+
SAFE_BRANCH=$(echo "$BRANCH_NAME" | sed 's/[^a-zA-Z0-9-]/-/g')
168+
169+
git fetch origin gh-pages:gh-pages
170+
git config user.name github-actions
171+
git config user.email github-actions@github.com
172+
git checkout gh-pages
173+
174+
if [ -d "preview/$SAFE_BRANCH" ]; then
175+
rm -rf preview/$SAFE_BRANCH
176+
git add -A preview/
177+
git commit -m "Remove preview: $BRANCH_NAME [ci skip]"
178+
git remote set-url origin https://${{ github.token }}@github.com/${{ github.repository_owner }}/openworm_docs.git
179+
git push origin gh-pages
180+
fi

0 commit comments

Comments
 (0)