-
Notifications
You must be signed in to change notification settings - Fork 11
345 lines (293 loc) · 14 KB
/
build.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
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
name: Build and publish artifacts
on:
push:
branches:
- main
- ci/test*
release:
types:
- published
jobs:
variables:
name: Set auxiliary variables
runs-on: ubuntu-latest
outputs:
pyodide_distro_name: ${{ steps.var_distro.outputs.pyodide_distro_name}}
should_build_pyodide: ${{ steps.var_distro.outputs.should_build_pyodide}}
pyodide_distro_url: ${{ steps.var_distro.outputs.pyodide_distro_url}}
is_release: ${{ steps.var_release.outputs.is_release }}
latest_version_label: ${{ steps.var_release.outputs.latest_version_label }}
steps:
- name: Setting variables for the Pyodide distro
uses: actions/github-script@v7
id: var_distro
env:
# Rebuilding the distro if any of these files have changed since the last build
PYODIDE_DISTRO_SRC: '["pyodide/packages/",
"pyodide/build.py",
"pyodide/package-patch.json.template",
"pyodide/README.md",
"draco/",
"poetry.lock"]'
with:
script: |
const shaShort = context.sha.substring(0, 7);
// Determining whether the Pyodide distro should be built.
// We can skip the build if an artifact with the same name already exists (`distroForThisShaExists`)
// OR
// If files relevant to the Pyodide build have not changed since the last Pyodide build (`distroDepDidNotChange`)
const artifacts = await github.rest.actions.listArtifactsForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
per_page: 100
}).then(({ data }) => data.artifacts);
// Extracting the commit SHA from the latest artifact name so that we can do some git diffing
const pyodideArtifacts = artifacts.filter(artifact => artifact.name.startsWith('pyodide-'));
const latestPyodideArtifact = pyodideArtifacts.sort((a, b) => b.updated_at - a.updated_at)[0];
core.info(`Latest Pyodide artifact: ${latestPyodideArtifact.name}`);
const latestPyodideShaShort = latestPyodideArtifact.name.split('-')[1];
const distroForThisShaExists = shaShort === latestPyodideShaShort;
if (distroForThisShaExists) {
core.setOutput('should_build_pyodide', false);
core.setOutput('pyodide_distro_name', latestPyodideArtifact.name);
core.setOutput('pyodide_distro_url', latestPyodideArtifact.archive_download_url);
return;
}
// List the file names that have changed since the last Pyodide build
const filesChangedBetweenCommits = await github.rest.repos.compareCommits({
owner: context.repo.owner,
repo: context.repo.repo,
base: latestPyodideShaShort,
head: shaShort
}).then(({ data }) => (data.files || []).map((file) => file.filename))
core.info(`Files changed between ${latestPyodideShaShort} and ${shaShort}: ${filesChangedBetweenCommits}`);
// Comparing changed files against the list of files that should trigger a rebuild
// We have directories in the list, so if any of the changed files are in one of those, we should rebuild
const pyodideDistroSrc = JSON.parse(process.env.PYODIDE_DISTRO_SRC);
const distroDepDidNotChange = filesChangedBetweenCommits.every(file => !pyodideDistroSrc.some(src => file.startsWith(src)));
if (distroDepDidNotChange) {
core.setOutput('should_build_pyodide', false);
core.setOutput('pyodide_distro_name', latestPyodideArtifact.name);
core.setOutput('pyodide_distro_url', latestPyodideArtifact.archive_download_url);
return;
}
// If we've made it this far, we should build the Pyodide distro from the current commit
core.setOutput('should_build_pyodide', true);
core.setOutput('pyodide_distro_name', `pyodide-${shaShort}`);
core.setOutput('pyodide_distro_url', null);
- name: Setting variables for the release
uses: actions/github-script@v7
id: var_release
with:
script: |
// Setting whether this is a release, indicating that the Pyodide distro
// should be pinned as a release artifact & uploaded to NPM
const isRelease = context.eventName === 'release';
core.setOutput('is_release', isRelease);
// Getting the latest version label by reading the 'version = {version}' line of 'pyproject.toml'
const latestVersionLabel = await github.rest.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: 'pyproject.toml'
}).then(({ data }) => {
const pyprojectToml = Buffer.from(data.content, 'base64').toString();
const versionLine = pyprojectToml.split('\n').find(line => line.startsWith('version = '));
return versionLine.split(' = ')[1].replace(/"/g, '');
});
core.setOutput('latest_version_label', latestVersionLabel);
- name: Summarize outputs
run: |
echo "pyodide_distro_name: ${{ steps.var_distro.outputs.pyodide_distro_name}}"
echo "should_build_pyodide: ${{ steps.var_distro.outputs.should_build_pyodide}}"
echo "pyodide_distro_url: ${{ steps.var_distro.outputs.pyodide_distro_url}}"
echo "is_release: ${{ steps.var_release.outputs.is_release }}"
echo "latest_version_label: ${{ steps.var_release.outputs.latest_version_label }}"
pyodide:
needs: [variables]
name: Build Pyodide Distro and upload as an artifact
runs-on: ubuntu-latest
env:
PYODIDE_DISTRO_NAME: ${{ needs.variables.outputs.pyodide_distro_name }}
SHOULD_BUILD_PYODIDE: ${{ needs.variables.outputs.should_build_pyodide }}
PYODIDE_DISTRO_URL: ${{ needs.variables.outputs.pyodide_distro_url }}
# GitHub actions does not allow early exiting from a job, so we have to do it manually using `if`s
steps:
- name: Skipping Pyodide build
if: ${{ env.SHOULD_BUILD_PYODIDE == 'false' }}
run: echo "Skipping Pyodide build, will download distro from ${{ env.PYODIDE_DISTRO_URL }}"
- name: Checkout repository
if: ${{ env.SHOULD_BUILD_PYODIDE == 'true' }}
uses: actions/checkout@v4
- name: Install poetry
if: ${{ env.SHOULD_BUILD_PYODIDE == 'true' }}
run: pipx install poetry
- name: Setup Python
if: ${{ env.SHOULD_BUILD_PYODIDE == 'true' }}
uses: actions/setup-python@v4
with:
python-version-file: ".python-version"
cache: "poetry"
- name: Install dependencies
if: ${{ env.SHOULD_BUILD_PYODIDE == 'true' }}
run: poetry install --with web
- name: Make Pyodide distribution
if: ${{ env.SHOULD_BUILD_PYODIDE == 'true' }}
run: make pyodide-build
- name: Copy Pyodide README.md to dist folder
if: ${{ env.SHOULD_BUILD_PYODIDE == 'true' }}
run: cp pyodide/README.md pyodide/pyodide-src/dist
- name: Upload Pyodide distribution
if: ${{ env.SHOULD_BUILD_PYODIDE == 'true' }}
uses: actions/upload-artifact@v3
with:
name: ${{ env.PYODIDE_DISTRO_NAME }}
path: pyodide/pyodide-src/dist
docs:
# No need to rebuild the docs if this is a release
if: ${{ needs.variables.outputs.is_release == 'false' }}
needs: [variables, pyodide]
name: Build Jupyter Book with Jupyter Lite and publish to GitHub Pages
runs-on: ubuntu-latest
env:
SHOULD_BUILD_PYODIDE: ${{ needs.variables.outputs.should_build_pyodide }}
PYODIDE_DISTRO_NAME: ${{ needs.variables.outputs.pyodide_distro_name }}
PYODIDE_DISTRO_URL: ${{ needs.variables.outputs.pyodide_distro_url }}
# This job is responsible for the GH Pages deployment
environment:
name: github-pages
url: ${{ steps.gh_pages_deployment.outputs.page_url }}
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install poetry
run: pipx install poetry
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version-file: ".python-version"
cache: "poetry"
- name: Install dependencies
run: poetry install --with web
# Required to execute notebooks with Python modules wrapping Node.js modules, such as Draco 1
- name: Setup Node
uses: actions/setup-node@v4
# Installing system dependencies required by notebooks to be executed
- name: Install system dependencies
run: sudo apt-get update && sudo apt-get install -y gringo
- name: Make book
run: make book
# Downloads to the local folder `pyodide/pyodide-src/dist` which
# gets picked up by `make jupyterlite-build` and embedded into the Jupyter Lite build
- name: Download freshly built Pyodide distribution
if: ${{ env.SHOULD_BUILD_PYODIDE == 'true' }}
uses: actions/download-artifact@v3
with:
name: ${{ env.PYODIDE_DISTRO_NAME }}
path: pyodide/pyodide-src/dist
# Downloading the prebuilt Pyodide distro using the URL of a previous artifact
- name: Download prebuilt Pyodide distro
if: ${{ env.SHOULD_BUILD_PYODIDE == 'false' }}
env:
# Needed for `gh` call
GH_TOKEN: ${{ github.token }}
# Download and extract the distro to the folder `pyodide/pyodide-src/dist`
# into which a fresh distro would be built. This is folder is going to be
# scanned by `make jupyterlite-build` for a local copy-pasteable distro
run: |
gh api ${{ env.PYODIDE_DISTRO_URL }} > ${{ env.PYODIDE_DISTRO_NAME }}.zip
mkdir -p pyodide/pyodide-src
unzip ${{ env.PYODIDE_DISTRO_NAME }}.zip -d pyodide/pyodide-src/dist
- name: Make JupyterLite
run: make jupyterlite-build
- name: Embed JupyterLite into docs
run: cp -r dist/jupyterlite docs/_build/html/jupyterlite
- name: Display HTML bundle tree
run: find docs/_build/html/ | sed -e "s/[^-][^\/]*\// |/g" -e "s/|\([^ ]\)/|-\1/"
- name: Setup Pages
uses: actions/configure-pages@v3
- name: Upload Documentation Site Artifact
uses: actions/upload-pages-artifact@v2
with:
path: ./docs/_build/html
- name: Deploy to GitHub Pages
id: gh_pages_deployment
uses: actions/deploy-pages@v2
pin_distro:
if: ${{ needs.variables.outputs.is_release == 'true' }}
needs: [variables, pyodide]
name: Pin Pyodide distribution as a release artifact
runs-on: ubuntu-latest
env:
SHOULD_BUILD_PYODIDE: ${{ needs.variables.outputs.should_build_pyodide }}
PYODIDE_DISTRO_NAME: ${{ needs.variables.outputs.pyodide_distro_name }}
PYODIDE_DISTRO_URL: ${{ needs.variables.outputs.pyodide_distro_url }}
PYODIDE_TAR_NAME: pyodide-${{ needs.variables.outputs.latest_version_label }}.tar.gz
GH_TOKEN: ${{ github.token }}
steps:
- name: Checkout repository so that `gh` can be used
uses: actions/checkout@v4
# This way we won't have conflicts when extracting the prebuilt distro
# We do not need repo files here anyway, we just checked out the repo
# so that the `gh` CLI can be used
- name: Remove existing `pyodide` dir from repo
run: rm -rf pyodide
- name: Download freshly built Pyodide distribution
if: ${{ env.SHOULD_BUILD_PYODIDE == 'true' }}
uses: actions/download-artifact@v3
with:
name: ${{ env.PYODIDE_DISTRO_NAME }}
path: pyodide
- name: Download prebuilt Pyodide distro
if: ${{ env.SHOULD_BUILD_PYODIDE == 'false' }}
run: |
gh api ${{ env.PYODIDE_DISTRO_URL }} > ${{ env.PYODIDE_DISTRO_NAME }}.zip
unzip ${{ env.PYODIDE_DISTRO_NAME }}.zip -d pyodide
ls -l pyodide
- name: Create tarball from Pyodide distribution
run: tar -czvf $PYODIDE_TAR_NAME -C pyodide .
- name: Upload Pyodide distribution as release asset
run: gh release upload ${{ github.event.release.tag_name }} $PYODIDE_TAR_NAME
npm_publish:
if: ${{ needs.variables.outputs.is_release == 'true' }}
needs: [variables, pyodide]
name: Publish Pyodide distribution to NPM
runs-on: ubuntu-latest
env:
SHOULD_BUILD_PYODIDE: ${{ needs.variables.outputs.should_build_pyodide }}
# E.g.: pyodide-08ecf9c
PYODIDE_DISTRO_NAME: ${{ needs.variables.outputs.pyodide_distro_name }}
# https://api.github.com/repos/{owner}/{repo}/actions/artifacts/{artifact_id}/{archive_format}
PYODIDE_DISTRO_URL: ${{ needs.variables.outputs.pyodide_distro_url }}
NODE_VERSION: 16
NPM_REGISTRY_URL: https://registry.npmjs.org
steps:
- name: Download freshly built Pyodide distribution
if: ${{ env.SHOULD_BUILD_PYODIDE == 'true' }}
uses: actions/download-artifact@v3
with:
name: ${{ env.PYODIDE_DISTRO_NAME }}
path: pyodide
# Downloading the prebuilt Pyodide distro using the URL of a previous artifact
- name: Download prebuilt Pyodide distro
if: ${{ env.SHOULD_BUILD_PYODIDE == 'false' }}
env:
# Needed for `gh` call
GH_TOKEN: ${{ github.token }}
run: |
gh api ${{ env.PYODIDE_DISTRO_URL }} > ${{ env.PYODIDE_DISTRO_NAME }}.zip
unzip ${{ env.PYODIDE_DISTRO_NAME }}.zip -d pyodide
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
registry-url: ${{ env.NPM_REGISTRY_URL }}
- name: Publish Pyodide distribution to NPM
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
working-directory: pyodide
run: npm publish