-
Notifications
You must be signed in to change notification settings - Fork 9
167 lines (144 loc) · 5.08 KB
/
deploy-book-ghpages.yml
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
name: deploy-book
# Based on:
# https://jupyterbook.org/en/stable/publish/gh-pages.html
# Run on a push to any branch, then deploy books from all branches.
on:
push:
branches:
- '**'
# If your git repository has the Jupyter Book within some-subfolder next to
# unrelated files, you can make this run only if a file within that specific
# folder has been modified.
#
# paths:
# - some-subfolder/**
workflow_dispatch:
# Inherit configuration variables in the environment, or assign default values.
# Configuration variables may be set as explained here:
# https://docs.github.com/en/actions/learn-github-actions/variables#creating-configuration-variables-for-a-repository
env:
# `main` as primary by default, advised to make it `published` to start using draft-publish-workflow
PRIMARY_BRANCH: ${{ vars.PRIMARY_BRANCH != '' && vars.PRIMARY_BRANCH || 'main' }}
# Space-separated list of alias-rules, e.g. 'draft:main alias:really-long-branch-name'
# By default, `draft` links to `main`. Advised to link `book` to `publish`
# If no aliases are wanted, BRANCH_ALIASES may be set to ' ' (space).
BRANCH_ALIASES: ${{ vars.BRANCH_ALIASES != '' && vars.BRANCH_ALIASES || 'draft:main' }}
# Space-separated list of branch names, e.g. 'main second third'.
# By default, deploy all branches. This is indicated by '*'.
BRANCHES_TO_DEPLOY: ${{ vars.BRANCHES_TO_DEPLOY != '' && vars.BRANCHES_TO_DEPLOY || '*' }}
jobs:
get-branches:
runs-on: ubuntu-latest
outputs:
branches: ${{ steps.set-branches.outputs.branches }}
permissions:
contents: read
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- id: set-branches
name: Set branches
run: |
if [ "$BRANCHES_TO_DEPLOY" == '*' ]; then
branches=$(git branch -r | sed 's,\s*origin/,,g' | jq -Rn '[inputs]')
else
branches=$(echo "$BRANCHES_TO_DEPLOY" | tr ' ' '\n' | grep -E '\S' | jq -Rn '[inputs]')
fi
echo "branches=$(echo $branches)" >> $GITHUB_OUTPUT
build-books:
runs-on: ubuntu-latest
needs: get-branches
if: ${{ needs.get-branches.outputs.branches != '[]' }}
permissions:
pages: write
id-token: write
strategy:
# If one branch fails, we may still want to deploy the other
fail-fast: false
matrix:
branch: ${{ fromJson(needs.get-branches.outputs.branches) }}
steps:
- name: Checkout to branch
uses: actions/checkout@v4
with:
ref: ${{ matrix.branch }}
- name: Cache page build
id: cache-html
uses: actions/cache@v4
with:
path: "book/_build/html"
key: html-build-${{ hashFiles('book/**', 'figures/**', 'requirements.txt') }}
- if: ${{ steps.cache-html.outputs.cache-hit != 'true' }}
name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: 3.11
cache: 'pip'
- if: ${{ steps.cache-html.outputs.cache-hit != 'true' }}
name: Install dependencies
run: |
pip install -r requirements.txt
- if: ${{ steps.cache-html.outputs.cache-hit != 'true' }}
name: Preprocess & build the book from branch
run: |
echo $PATH
if [ ${{matrix.branch}} == $PRIMARY_BRANCH ]; then
teachbooks build --publish book/
else
teachbooks build book/
fi
- name: Clean branch name of disallowed characters
run: |
echo "MATRIX_BRANCH_NAME_CLEAN=$(echo ${{ matrix.branch }} | tr '/":<>|*?\/\\' '-')" >> $GITHUB_ENV
- name: Upload the built book HTML as an artifact
uses: actions/upload-artifact@v4
with:
name: ${{ env.MATRIX_BRANCH_NAME_CLEAN }}
path: "book/_build/html"
deploy-books:
# Run after build-books, even if it failed
if: always()
needs: build-books
runs-on: ubuntu-latest
permissions:
pages: write
id-token: write
steps:
- uses: actions/checkout@v4
- run: |
mkdir final/
- name: Download all book artifacts
uses: actions/download-artifact@v4
with:
path: "final/"
- name: Copy primary book to root, fail if names conflict
timeout-minutes: 1
run: |
GLOBIGNORE=".:.."
# -i leads to a prompt in case of conflict, => a timeout
if [ -d final/$PRIMARY_BRANCH ]; then
cp -irv final/$PRIMARY_BRANCH/* final/
fi
ls -a final/
- name: Symlink branch aliases
run: |
echo $BRANCH_ALIASES | tr ' ' '\n' | grep -E '\S' |
while IFS=':' read -r key value; do
# If the target branch is to be deployed, make symlink to it.
if [ "$BRANCHES_TO_DEPLOY" == "*" ] || echo $BRANCHES_TO_DEPLOY | tr ' ' '\n' | grep "^$value$"; then
echo link $key "->" $value
ln -s $value final/$key
fi
done
- name: Upload final Pages artifact
uses: actions/upload-pages-artifact@v3
with:
path: "final/"
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
permissions:
contents: read
pages: write
id-token: write