|
1 | | -# Simple workflow for deploying static content to GitHub Pages |
| 1 | +# Workflow for deploying versioned documentation to GitHub Pages |
2 | 2 | name: Deploy Maven Site to Pages |
3 | 3 |
|
4 | 4 | on: |
5 | | - # Runs on pushes targeting the default branch |
| 5 | + # Runs on pushes targeting the default branch (publishes to /snapshot/) |
6 | 6 | push: |
7 | 7 | branches: ["main"] |
| 8 | + paths: |
| 9 | + - 'src/site/**' |
| 10 | + - 'src/main/java/**' |
| 11 | + - 'pom.xml' |
| 12 | + - '.github/workflows/site.yml' |
8 | 13 |
|
9 | | - # Allows you to run this workflow manually from the Actions tab |
| 14 | + # Runs on release publish (publishes to / and /vX.Y.Z/) |
| 15 | + release: |
| 16 | + types: [published] |
| 17 | + |
| 18 | + # Allows manual trigger |
10 | 19 | workflow_dispatch: |
| 20 | + inputs: |
| 21 | + publish_as_latest: |
| 22 | + description: 'Also publish as latest (root path)?' |
| 23 | + type: boolean |
| 24 | + default: false |
11 | 25 |
|
12 | 26 | # Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages |
13 | 27 | permissions: |
14 | | - contents: read |
| 28 | + contents: write |
15 | 29 | pages: write |
16 | 30 | id-token: write |
17 | 31 |
|
18 | | -# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. |
19 | | -# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. |
| 32 | +# Allow only one concurrent deployment |
20 | 33 | concurrency: |
21 | 34 | group: "pages" |
22 | 35 | cancel-in-progress: false |
23 | 36 |
|
24 | 37 | jobs: |
25 | | - # Build Maven Site |
26 | | - build: |
| 38 | + build-and-deploy: |
27 | 39 | runs-on: ubuntu-latest |
| 40 | + environment: |
| 41 | + name: github-pages |
| 42 | + url: ${{ steps.deployment.outputs.page_url }} |
28 | 43 | steps: |
29 | 44 | - name: Checkout |
30 | 45 | uses: actions/checkout@v6 |
| 46 | + with: |
| 47 | + ref: ${{ github.event_name == 'release' && github.event.release.tag_name || github.ref }} |
| 48 | + fetch-depth: 0 |
31 | 49 |
|
32 | 50 | - name: Set up JDK 17 |
33 | 51 | uses: actions/setup-java@v5 |
34 | 52 | with: |
35 | 53 | java-version: '17' |
36 | 54 | distribution: 'temurin' |
37 | 55 |
|
| 56 | + - name: Determine version and paths |
| 57 | + id: version |
| 58 | + run: | |
| 59 | + if [[ "${{ github.event_name }}" == "release" ]]; then |
| 60 | + VERSION="${{ github.event.release.tag_name }}" |
| 61 | + VERSION="${VERSION#v}" # Remove 'v' prefix if present |
| 62 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 63 | + echo "is_release=true" >> $GITHUB_OUTPUT |
| 64 | + echo "deploy_path=$VERSION" >> $GITHUB_OUTPUT |
| 65 | + else |
| 66 | + # For main branch pushes |
| 67 | + CURRENT_VERSION=$(./mvnw help:evaluate -Dexpression=project.version -q -DforceStdout) |
| 68 | + echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT |
| 69 | + echo "is_release=false" >> $GITHUB_OUTPUT |
| 70 | + echo "deploy_path=snapshot" >> $GITHUB_OUTPUT |
| 71 | + fi |
| 72 | +
|
38 | 73 | - name: Build Maven Site with Javadoc |
39 | 74 | run: ./mvnw clean site -DskipTests -Dcheckstyle.skip=true |
40 | 75 |
|
| 76 | + - name: Checkout gh-pages branch |
| 77 | + uses: actions/checkout@v6 |
| 78 | + with: |
| 79 | + ref: gh-pages |
| 80 | + path: gh-pages |
| 81 | + continue-on-error: true |
| 82 | + |
| 83 | + - name: Initialize gh-pages if needed |
| 84 | + run: | |
| 85 | + if [ ! -d "gh-pages" ]; then |
| 86 | + mkdir -p gh-pages |
| 87 | + cd gh-pages |
| 88 | + git init |
| 89 | + git checkout -b gh-pages |
| 90 | + fi |
| 91 | +
|
| 92 | + - name: Prepare versioned documentation |
| 93 | + run: | |
| 94 | + DEPLOY_PATH="${{ steps.version.outputs.deploy_path }}" |
| 95 | + |
| 96 | + # Remove old version of this specific path (if exists) |
| 97 | + rm -rf "gh-pages/$DEPLOY_PATH" |
| 98 | + |
| 99 | + # Copy new site to versioned path |
| 100 | + mkdir -p "gh-pages/$DEPLOY_PATH" |
| 101 | + cp -r target/site/* "gh-pages/$DEPLOY_PATH/" |
| 102 | + |
| 103 | + # For releases, also update the root (latest) |
| 104 | + if [[ "${{ steps.version.outputs.is_release }}" == "true" ]] || [[ "${{ inputs.publish_as_latest }}" == "true" ]]; then |
| 105 | + # Preserve versioned folders when updating root |
| 106 | + find gh-pages -maxdepth 1 -type f -delete |
| 107 | + find gh-pages -maxdepth 1 -type d -name "[0-9]*" -prune -o -type d ! -name gh-pages ! -name snapshot ! -name .git -exec rm -rf {} + 2>/dev/null || true |
| 108 | + |
| 109 | + # Copy site to root (latest release) |
| 110 | + cp -r target/site/* gh-pages/ |
| 111 | + fi |
| 112 | + |
| 113 | + # Copy version index page from template |
| 114 | + cp .github/templates/versions.html gh-pages/versions.html |
| 115 | +
|
| 116 | + - name: Update version list |
| 117 | + run: | |
| 118 | + cd gh-pages |
| 119 | + |
| 120 | + # Find all version directories |
| 121 | + VERSIONS=$(find . -maxdepth 1 -type d -name "[0-9]*" | sed 's|./||' | sort -Vr) |
| 122 | + HAS_SNAPSHOT=$([ -d "snapshot" ] && echo "true" || echo "false") |
| 123 | + |
| 124 | + # Generate version links |
| 125 | + VERSION_HTML="" |
| 126 | + |
| 127 | + # Add latest (root) link |
| 128 | + VERSION_HTML+='<li><a href="./">Latest Release</a><span class="badge latest">latest</span></li>' |
| 129 | + |
| 130 | + # Add snapshot if exists |
| 131 | + if [ "$HAS_SNAPSHOT" = "true" ]; then |
| 132 | + VERSION_HTML+='<li><a href="./snapshot/">Development (main branch)</a><span class="badge snapshot">snapshot</span></li>' |
| 133 | + fi |
| 134 | + |
| 135 | + # Add versioned releases |
| 136 | + for v in $VERSIONS; do |
| 137 | + VERSION_HTML+="<li><a href=\"./$v/\">Version $v</a><span class=\"badge archived\">$v</span></li>" |
| 138 | + done |
| 139 | + |
| 140 | + # Update the versions.html using the placeholder |
| 141 | + sed -i "s|<!-- VERSION_LIST_PLACEHOLDER -->|$VERSION_HTML|" versions.html |
| 142 | +
|
| 143 | + - name: Deploy to GitHub Pages |
| 144 | + run: | |
| 145 | + cd gh-pages |
| 146 | + git config user.name "github-actions[bot]" |
| 147 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 148 | + git add -A |
| 149 | + git diff --staged --quiet || git commit -m "Deploy documentation: ${{ steps.version.outputs.deploy_path }}" |
| 150 | + git push origin gh-pages --force |
| 151 | + env: |
| 152 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 153 | + |
41 | 154 | - name: Setup Pages |
42 | 155 | uses: actions/configure-pages@v5 |
43 | 156 |
|
44 | 157 | - name: Upload artifact |
45 | 158 | uses: actions/upload-pages-artifact@v4 |
46 | 159 | with: |
47 | | - # Upload the generated site |
48 | | - path: 'target/site' |
| 160 | + path: 'gh-pages' |
49 | 161 |
|
50 | | - # Deploy to GitHub Pages |
51 | | - deploy: |
52 | | - environment: |
53 | | - name: github-pages |
54 | | - url: ${{ steps.deployment.outputs.page_url }} |
55 | | - runs-on: ubuntu-latest |
56 | | - needs: build |
57 | | - steps: |
58 | 162 | - name: Deploy to GitHub Pages |
59 | 163 | id: deployment |
60 | 164 | uses: actions/deploy-pages@v4 |
0 commit comments