|
| 1 | +name: Build and Deploy Documentation |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main ] |
| 6 | + tags: |
| 7 | + - 'v[0-9]*.[0-9]*.[0-9]*' |
| 8 | + pull_request: |
| 9 | + types: [opened, synchronize, reopened] |
| 10 | + |
| 11 | +jobs: |
| 12 | + build: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + permissions: |
| 15 | + contents: write |
| 16 | + steps: |
| 17 | + - name: Checkout repository |
| 18 | + uses: actions/checkout@v4 |
| 19 | + |
| 20 | + - name: Setup PDM |
| 21 | + uses: pdm-project/setup-pdm@v4 |
| 22 | + with: |
| 23 | + python-version: '3.12' |
| 24 | + |
| 25 | + - name: Install dependencies (default & doc) |
| 26 | + run: pdm install --group doc --frozen-lockfile |
| 27 | + |
| 28 | + - name: Build Documentation |
| 29 | + working-directory: docs |
| 30 | + run: pdm run make dirhtml |
| 31 | + |
| 32 | + - name: Determine deployment folder |
| 33 | + id: deploy_folder |
| 34 | + run: | |
| 35 | + echo "Determining deployment folder..." |
| 36 | + if [ "${{ github.event_name }}" = "pull_request" ]; then |
| 37 | + echo "Deploying to target pr/${{ github.event.number }}" |
| 38 | + echo "DEPLOY_DIR=pr/${{ github.event.number }}" >> $GITHUB_OUTPUT |
| 39 | + elif [[ "${{ github.ref }}" == refs/tags/* ]]; then |
| 40 | + echo "Deploying to target ${{ github.ref_name }}" |
| 41 | + echo "DEPLOY_DIR=${{ github.ref_name }}" >> $GITHUB_OUTPUT |
| 42 | + else |
| 43 | + echo "Deploying to target main" |
| 44 | + echo "DEPLOY_DIR=main" >> $GITHUB_OUTPUT |
| 45 | + fi |
| 46 | +
|
| 47 | + - name: Deploy Documentation to GitHub Pages |
| 48 | + uses: peaceiris/actions-gh-pages@v4 |
| 49 | + with: |
| 50 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 51 | + publish_dir: docs/build/dirhtml |
| 52 | + destination_dir: ${{ steps.deploy_folder.outputs.DEPLOY_DIR }} |
| 53 | + publish_branch: gh-pages |
| 54 | + |
| 55 | + - name: Update default site redirect (tag event only) |
| 56 | + if: startsWith(github.ref, 'refs/tags/') |
| 57 | + run: | |
| 58 | + # Extract the tag name from the GITHUB_REF variable. |
| 59 | + TAG_NAME=${GITHUB_REF##*/} |
| 60 | + # Create an index.html file that redirects to /<tag>/index.html. |
| 61 | + echo "<!DOCTYPE html><html><head><meta http-equiv=\"refresh\" content=\"0; url=./${TAG_NAME}/index.html\" /></head><body></body></html>" > docs/build/index.html |
| 62 | + # Clone the gh-pages branch using token authentication |
| 63 | + git clone --branch gh-pages https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }} gh-pages |
| 64 | + cp docs/build/index.html gh-pages/index.html |
| 65 | + cd gh-pages |
| 66 | + git config user.name "github-actions" |
| 67 | + git config user.email "github-actions@github.com" |
| 68 | + git add index.html |
| 69 | + git commit -m "Update default documentation redirect to tag ${GITHUB_REF##*/}" || echo "No changes to commit" |
| 70 | + git push origin gh-pages |
0 commit comments