|
5 | 5 | types: [created]
|
6 | 6 | pull_request:
|
7 | 7 | branches: [main]
|
| 8 | + push: |
| 9 | + branches: [main] |
8 | 10 |
|
9 | 11 | jobs:
|
10 | 12 | build:
|
11 | 13 | runs-on: ubuntu-latest
|
12 | 14 | steps:
|
13 | 15 | - uses: actions/checkout@v4
|
| 16 | + with: |
| 17 | + fetch-depth: 0 # Fetch all history for proper versioning |
| 18 | + |
14 | 19 | - uses: actions/setup-python@v5
|
15 | 20 | with:
|
16 | 21 | python-version: "3.13"
|
| 22 | + |
17 | 23 | - name: Install dependencies
|
18 | 24 | run: pip install -e ".[dev]"
|
| 25 | + |
| 26 | + - name: Determine version and build type |
| 27 | + id: version |
| 28 | + run: | |
| 29 | + if [[ "${{ github.event_name }}" == "release" ]]; then |
| 30 | + echo "VERSION=stable" >> $GITHUB_OUTPUT |
| 31 | + echo "DIR=stable" >> $GITHUB_OUTPUT |
| 32 | + echo "TAG=${{ github.ref_name }}" >> $GITHUB_OUTPUT |
| 33 | + elif [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == "refs/heads/main" ]]; then |
| 34 | + echo "VERSION=latest" >> $GITHUB_OUTPUT |
| 35 | + echo "DIR=latest" >> $GITHUB_OUTPUT |
| 36 | + echo "TAG=main" >> $GITHUB_OUTPUT |
| 37 | + else |
| 38 | + echo "VERSION=latest" >> $GITHUB_OUTPUT |
| 39 | + echo "DIR=latest" >> $GITHUB_OUTPUT |
| 40 | + echo "TAG=pr-${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT |
| 41 | + fi |
| 42 | +
|
19 | 43 | - name: Generate versions.json
|
20 | 44 | run: |
|
| 45 | + # Create a more comprehensive versions.json |
21 | 46 | cat <<EOF > docs/versions.json
|
22 | 47 | [
|
23 | 48 | {
|
24 | 49 | "version": "latest",
|
25 |
| - "url": "/xarray-dataclass/latest/" |
| 50 | + "url": "https://xarray-contrib.github.io/xarray-dataclass/latest/", |
| 51 | + "name": "latest" |
26 | 52 | },
|
27 | 53 | {
|
28 | 54 | "version": "stable",
|
29 |
| - "url": "/xarray-dataclass/stable/" |
| 55 | + "url": "https://xarray-contrib.github.io/xarray-dataclass/stable/", |
| 56 | + "name": "stable" |
30 | 57 | }
|
31 | 58 | ]
|
32 | 59 | EOF
|
| 60 | +
|
33 | 61 | - name: Build docs
|
34 | 62 | run: |
|
35 |
| - # Set DOCS_VERSION env var according to event |
36 |
| - if [[ "${{ github.event_name }}" == "release" ]]; then |
37 |
| - export DOCS_VERSION=stable |
38 |
| - else |
39 |
| - export DOCS_VERSION=latest |
40 |
| - fi |
| 63 | + # Set DOCS_VERSION env var according to determined version |
| 64 | + export DOCS_VERSION=${{ steps.version.outputs.VERSION }} |
| 65 | + echo "Building documentation for version: $DOCS_VERSION" |
41 | 66 |
|
| 67 | + # Build the documentation |
42 | 68 | python -m sphinx -b html docs docs/_build
|
| 69 | +
|
43 | 70 | - name: Copy versions.json to build output
|
44 | 71 | run: cp docs/versions.json docs/_build/
|
| 72 | + |
| 73 | + - name: Create 404 page for GitHub Pages |
| 74 | + if: ${{ github.event_name == 'release' || (github.event_name == 'push' && github.ref == 'refs/heads/main') }} |
| 75 | + run: | |
| 76 | + # Create a symlink of the 404.html file in the root directory for GitHub Pages |
| 77 | + # This ensures that the 404 page is used for all missing pages across all versions |
| 78 | + cp docs/_build/404.html docs/_build/404.html.bak |
| 79 | + cat <<EOF > docs/_build/404.html |
| 80 | + <!DOCTYPE html> |
| 81 | + <html> |
| 82 | + <head> |
| 83 | + <meta charset="UTF-8"> |
| 84 | + <meta http-equiv="refresh" content="0; url=./latest/404.html"> |
| 85 | + <title>Page Not Found</title> |
| 86 | + </head> |
| 87 | + <body> |
| 88 | + <p>If you are not redirected automatically, follow this <a href="./latest/404.html">link to the 404 page</a>.</p> |
| 89 | + </body> |
| 90 | + </html> |
| 91 | + EOF |
| 92 | + # Restore the original 404.html in the version directory |
| 93 | + mv docs/_build/404.html.bak docs/_build/404.html |
| 94 | +
|
| 95 | + - name: Create redirect index.html |
| 96 | + if: ${{ github.event_name == 'release' || (github.event_name == 'push' && github.ref == 'refs/heads/main') }} |
| 97 | + run: | |
| 98 | + # Create a root index.html that redirects to the stable version (or latest if no stable exists) |
| 99 | + cat <<EOF > docs/_build/index.html |
| 100 | + <!DOCTYPE html> |
| 101 | + <html> |
| 102 | + <head> |
| 103 | + <meta charset="UTF-8"> |
| 104 | + <meta http-equiv="refresh" content="0; url=./${{ steps.version.outputs.DIR }}/"> |
| 105 | + <title>Redirect to ${{ steps.version.outputs.VERSION }} documentation</title> |
| 106 | + </head> |
| 107 | + <body> |
| 108 | + <p>If you are not redirected automatically, follow this <a href="./${{ steps.version.outputs.DIR }}/">link to the ${{ steps.version.outputs.VERSION }} documentation</a>.</p> |
| 109 | + </body> |
| 110 | + </html> |
| 111 | + EOF |
| 112 | +
|
45 | 113 | - name: Deploy docs
|
46 | 114 | uses: peaceiris/actions-gh-pages@v3
|
47 | 115 | with:
|
48 | 116 | github_token: ${{ secrets.GITHUB_TOKEN }}
|
49 | 117 | publish_dir: ./docs/_build
|
50 |
| - destination_dir: ${{ github.event_name == 'release' && 'stable' || 'latest' }} |
| 118 | + destination_dir: ${{ steps.version.outputs.DIR }} |
0 commit comments