Skip to content

Commit d4176c5

Browse files
ci: Move netlify build into GitHub action (#5153)
* Move netlify build into GitHub action * update rust nightly * update rust nightly * add back lychee * move deploy to separate workflow * add deployment secrets * add safety comment * move netlify cli install --------- Co-authored-by: David Hewitt <mail@davidhewitt.dev>
1 parent 4be8099 commit d4176c5

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
name: netlify-build
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
release:
9+
types: [published]
10+
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
13+
cancel-in-progress: true
14+
15+
env:
16+
CARGO_TERM_COLOR: always
17+
18+
jobs:
19+
guide-build:
20+
runs-on: ubuntu-latest
21+
outputs:
22+
tag_name: ${{ steps.prepare_tag.outputs.tag_name }}
23+
steps:
24+
- uses: actions/checkout@v4
25+
- uses: actions/setup-python@v5
26+
with:
27+
python-version: '3.12'
28+
- uses: actions/setup-node@v4
29+
with:
30+
node-version: '20.x'
31+
registry-url: 'https://registry.npmjs.org'
32+
33+
- uses: dtolnay/rust-toolchain@nightly
34+
35+
- name: Use nightly
36+
run: |
37+
rustup update nightly
38+
rustup default nightly
39+
40+
- name: Setup mdBook
41+
uses: taiki-e/install-action@v2
42+
with:
43+
tool: mdbook, lychee, mdbook-linkcheck
44+
45+
- name: Prepare tag
46+
id: prepare_tag
47+
run: |
48+
TAG_NAME="${GITHUB_REF##*/}"
49+
echo "::set-output name=tag_name::${TAG_NAME}"
50+
51+
# Start with the current guides.
52+
- name: Get the guide
53+
run: |
54+
wget -qc https://github.com/PyO3/pyo3/archive/gh-pages.tar.gz -O - | tar -xz
55+
mv pyo3-gh-pages netlify_build
56+
57+
# Set redirects.
58+
- name: Set redirects
59+
run: |
60+
source .netlify/redirect.sh
61+
62+
# This builds the book and docs in netlify_build/main
63+
- name: Build the guide and public docs
64+
run: |
65+
python -m pip install --upgrade pip && pip install nox towncrier
66+
towncrier build --yes --version Unreleased --date TBC
67+
nox -s check-guide
68+
mv target/guide/ netlify_build/main/
69+
nox -s docs
70+
mv target/doc netlify_build/main/doc/
71+
echo "<meta http-equiv=refresh content=0;url=pyo3/>" > netlify_build/main/doc/index.html
72+
nox -s docs -- nightly internal
73+
mkdir -p netlify_build/internal
74+
mv target/doc netlify_build/internal/
75+
76+
# Upload the built site as an artifact for deploy workflow to consume
77+
- name: Upload Build Artifact
78+
uses: actions/upload-artifact@v4
79+
with:
80+
name: site
81+
path: ./netlify_build
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# This runs as a separate job because it needs to run on the `workflow_run` event
2+
# in order to access the netlify secrets.
3+
#
4+
# This is safe because this doesn't run arbitrary code from PRs.
5+
6+
name: netlify-deploy
7+
8+
on:
9+
workflow_run:
10+
workflows: ['netlify-build']
11+
types:
12+
- completed
13+
14+
env:
15+
CARGO_TERM_COLOR: always
16+
17+
jobs:
18+
deploy:
19+
runs-on: ubuntu-latest
20+
if: github.event.workflow_run.conclusion == 'success'
21+
environment: netlify
22+
23+
steps:
24+
- name: Download Build Artifact
25+
uses: actions/download-artifact@v4
26+
with:
27+
name: site
28+
github-token: ${{ secrets.GITHUB_TOKEN }}
29+
run-id: ${{ github.event.workflow_run.id }}
30+
31+
- name: Install netlify-cli
32+
run: |
33+
npm install -g netlify-cli
34+
35+
- name: Deploy to Netlify
36+
run: |
37+
ls -l netlify_build/
38+
DEBUG=* netlify deploy \
39+
--site ${{ secrets.NETLIFY_SITE_ID }} \
40+
--auth ${{ secrets.NETLIFY_TOKEN }} \
41+
${{ github.event.workflow_run.head_branch == 'main' && '--prod' || '' }} \

.netlify/redirect.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Add redirect for each documented version
2+
set +x # these loops get very spammy and fill the deploy log
3+
4+
for d in netlify_build/v*; do
5+
version="${d/netlify_build\/v/}"
6+
echo "/v$version/doc/* https://docs.rs/pyo3/$version/:splat" >> netlify_build/_redirects
7+
if [ $version != $PYO3_VERSION ]; then
8+
# for old versions, mark the files in the latest version as the canonical URL
9+
for file in $(find $d -type f); do
10+
file_path="${file/$d\//}"
11+
# remove index.html and/or .html suffix to match the page URL on the
12+
# final netlfiy site
13+
url_path="$file_path"
14+
if [[ $file_path == index.html ]]; then
15+
url_path=""
16+
elif [[ $file_path == *.html ]]; then
17+
url_path="${file_path%.html}"
18+
fi
19+
echo "/v$version/$url_path" >> netlify_build/_headers
20+
if test -f "netlify_build/v$PYO3_VERSION/$file_path"; then
21+
echo " Link: <https://pyo3.rs/v$PYO3_VERSION/$url_path>; rel=\"canonical\"" >> netlify_build/_headers
22+
else
23+
# this file doesn't exist in the latest guide, don't index it
24+
echo " X-Robots-Tag: noindex" >> netlify_build/_headers
25+
fi
26+
done
27+
fi
28+
done
29+
30+
# Add latest redirect
31+
echo "/latest/* /v${PYO3_VERSION}/:splat 302" >> netlify_build/_redirects
32+
33+
# some backwards compatbiility redirects
34+
echo "/latest/building_and_distribution/* /latest/building-and-distribution/:splat 302" >> netlify_build/_redirects
35+
echo "/latest/building-and-distribution/multiple_python_versions/* /latest/building-and-distribution/multiple-python-versions:splat 302" >> netlify_build/_redirects
36+
echo "/latest/function/error_handling/* /latest/function/error-handling/:splat 302" >> netlify_build/_redirects
37+
echo "/latest/getting_started/* /latest/getting-started/:splat 302" >> netlify_build/_redirects
38+
echo "/latest/python_from_rust/* /latest/python-from-rust/:splat 302" >> netlify_build/_redirects
39+
echo "/latest/python_typing_hints/* /latest/python-typing-hints/:splat 302" >> netlify_build/_redirects
40+
echo "/latest/trait_bounds/* /latest/trait-bounds/:splat 302" >> netlify_build/_redirects
41+
42+
## Add landing page redirect
43+
if [ "${CONTEXT}" == "deploy-preview" ]; then
44+
echo "/ /main/" >> netlify_build/_redirects
45+
else
46+
echo "/ /v${PYO3_VERSION}/ 302" >> netlify_build/_redirects
47+
fi
48+
49+
set -x

0 commit comments

Comments
 (0)